emulator step
This commit is contained in:
parent
da804769c6
commit
80ff284c0d
|
@ -1,5 +1,7 @@
|
||||||
package chip8
|
package chip8
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MemorySize = 4096 // 4KB of memory
|
MemorySize = 4096 // 4KB of memory
|
||||||
StackSize = 16
|
StackSize = 16
|
||||||
|
@ -41,3 +43,20 @@ func (emulator *Emulator) Reset() {
|
||||||
emulator.Timer.Sound = 0
|
emulator.Timer.Sound = 0
|
||||||
copy(emulator.Memory[ProgramAddress:], emulator.ROM)
|
copy(emulator.Memory[ProgramAddress:], emulator.ROM)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (emulator *Emulator) Step() {
|
||||||
|
instruction := binary.BigEndian.Uint16(emulator.Memory[emulator.PC:])
|
||||||
|
|
||||||
|
switch instruction & 0xF000 {
|
||||||
|
case 0x1000: // 1nnn - JP addr
|
||||||
|
emulator.Jump(instruction & 0x0FFF)
|
||||||
|
case 0x2000: // 2nnn - CALL addr
|
||||||
|
emulator.Call(instruction & 0x0FFF)
|
||||||
|
case 0x3000: // 3xkk - SE Vx, byte
|
||||||
|
emulator.SkipEqual(uint8(instruction&0x0F00>>8), uint8(instruction&0x00FF))
|
||||||
|
case 0x4000: // 4xkk - SNE Vx, byte
|
||||||
|
emulator.SkipNotEqual(uint8(instruction&0x0F00>>8), uint8(instruction&0x00FF))
|
||||||
|
case 0x5000: // 5xy0 - SE Vx, Vy
|
||||||
|
emulator.SkipRegistersEqual(uint8(instruction&0x0F00>>8), uint8(instruction&0x00F0>>4))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,31 +9,50 @@ func (emulator *Emulator) Return() {
|
||||||
emulator.StackPop()
|
emulator.StackPop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (emulator *Emulator) Jump(addr uint16) {
|
// Jump to location nnn.
|
||||||
emulator.PC = addr
|
//
|
||||||
|
// The interpreter sets the program counter to nnn.
|
||||||
|
func (emulator *Emulator) Jump(nnn uint16) {
|
||||||
|
emulator.PC = nnn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (emulator *Emulator) Call(addr uint16) {
|
// Call subroutine at nnn.
|
||||||
|
//
|
||||||
|
// The interpreter increments the stack pointer, then puts the current PC
|
||||||
|
// on the top of the stack. The PC is then set to nnn.
|
||||||
|
func (emulator *Emulator) Call(nnn uint16) {
|
||||||
emulator.StackPush()
|
emulator.StackPush()
|
||||||
emulator.PC = addr
|
emulator.PC = nnn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (emulator *Emulator) SkipEqual(x uint8, value uint8) {
|
// Skip next instruction if Vx = kk.
|
||||||
if emulator.V[x] == value {
|
//
|
||||||
|
// The interpreter compares register Vx to kk, and if they are equal,
|
||||||
|
// increments the program counter by 2.
|
||||||
|
func (emulator *Emulator) SkipEqual(x uint8, kk uint8) {
|
||||||
|
if emulator.V[x] == kk {
|
||||||
emulator.PC += InstructionSize * 2
|
emulator.PC += InstructionSize * 2
|
||||||
} else {
|
} else {
|
||||||
emulator.PC += InstructionSize
|
emulator.PC += InstructionSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (emulator *Emulator) SkipNotEqual(x uint8, value uint8) {
|
// Skip next instruction if Vx != kk.
|
||||||
if emulator.V[x] != value {
|
//
|
||||||
|
// The interpreter compares register Vx to kk, and if they are not equal,
|
||||||
|
// increments the program counter by 2.
|
||||||
|
func (emulator *Emulator) SkipNotEqual(x uint8, kk uint8) {
|
||||||
|
if emulator.V[x] != kk {
|
||||||
emulator.PC += InstructionSize * 2
|
emulator.PC += InstructionSize * 2
|
||||||
} else {
|
} else {
|
||||||
emulator.PC += InstructionSize
|
emulator.PC += InstructionSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip next instruction if Vx = Vy.
|
||||||
|
//
|
||||||
|
// The interpreter compares register Vx to register Vy, and if they are equal,
|
||||||
|
// increments the program counter by 2.
|
||||||
func (emulator *Emulator) SkipRegistersEqual(x uint8, y uint8) {
|
func (emulator *Emulator) SkipRegistersEqual(x uint8, y uint8) {
|
||||||
if emulator.V[x] == emulator.V[y] {
|
if emulator.V[x] == emulator.V[y] {
|
||||||
emulator.PC += InstructionSize * 2
|
emulator.PC += InstructionSize * 2
|
||||||
|
|
Loading…
Reference in New Issue
Block a user