Implement instructions 0x0 and 0x1
This commit is contained in:
parent
f27ac7b560
commit
601636a0ad
19
creature.go
19
creature.go
@ -8,13 +8,24 @@ type Machine struct {
|
|||||||
pointer int
|
pointer int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute starts execution at the address specified by offset. The machine will
|
||||||
|
// run until a HALT instruction is encountered, or the end of program memory is
|
||||||
|
// reached.
|
||||||
func (machine *Machine) Execute (offset int) {
|
func (machine *Machine) Execute (offset int) {
|
||||||
machine.counter = offset
|
machine.counter = offset
|
||||||
|
|
||||||
for {
|
for {
|
||||||
switch machine.Program[machine.counter] {
|
switch machine.instruction() {
|
||||||
case 0x0:
|
case 0x0:
|
||||||
|
// PUSH
|
||||||
|
// push the next word in program memory onto the stack
|
||||||
|
machine.counter ++
|
||||||
|
machine.Push(machine.instruction())
|
||||||
|
|
||||||
case 0x1:
|
case 0x1:
|
||||||
|
// POP
|
||||||
|
// pop the top word off of the stack, and discard it
|
||||||
|
machine.Pop()
|
||||||
case 0x2:
|
case 0x2:
|
||||||
case 0x3:
|
case 0x3:
|
||||||
case 0x4:
|
case 0x4:
|
||||||
@ -34,6 +45,12 @@ func (machine *Machine) Execute (offset int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Instruction returns the current instruction in program memory.
|
||||||
|
func (machine *Machine) instruction () (instruction int) {
|
||||||
|
instruction = machine.Program[machine.counter]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// reallocateStack changes the size of the stack to something reasonable. This
|
// reallocateStack changes the size of the stack to something reasonable. This
|
||||||
// should be called then the stack pointer is really small compared to the
|
// should be called then the stack pointer is really small compared to the
|
||||||
// actual stack size, or the stack pointer is bigger than the stack.
|
// actual stack size, or the stack pointer is bigger than the stack.
|
||||||
|
Loading…
Reference in New Issue
Block a user