Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
53 lines (44 sloc) 1.07 KB
// This file is provided as example code to illustrate the following article:
// Feel free to do whatever with it!
package main
import (
type CPU struct {
A, F uint8
B, C uint8
D, E uint8
H, L uint8
SP uint16
PC uint16
func main() {
memory, err := ioutil.ReadFile("./dmg-rom.bin")
if err != nil {
cpu := CPU{}
for {
opcode := memory[cpu.PC]
switch opcode {
// ... other opcodes
case 0x31: // LD SP,d16
// Here, Go requires some type conversion to fit two
// uint8 into one uint16 and also to avoid grossly
// overflowing an uint8 by shifting it left 8 bits,
// which would just make it zero and yield 0x00fe
// instead of 0xfffe. That was a fun crash to debug!
cpu.SP = uint16(memory[cpu.PC]) | uint16(memory[cpu.PC+1])<<8
cpu.PC += 2
// ... other opcodes
fmt.Printf("Unknown opcode: %#2x\n", opcode)
fmt.Printf("cpu.PC=0x%04x\n", cpu.PC)
fmt.Printf("cpu.SP=0x%04x\n", cpu.SP)
You can’t perform that action at this time.