|
|
|
@ -38,21 +38,27 @@ func (h *Module) debug(format string, args ...interface{}) { |
|
|
|
|
fmt.Printf("[DEBUG] "+format+"\n", args...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) Step() { |
|
|
|
|
func (h *Module) Step() error { |
|
|
|
|
instruction := h.programStorage[h.programCounter()] |
|
|
|
|
h.increaseProgramCounter() |
|
|
|
|
if instruction == nil { |
|
|
|
|
return |
|
|
|
|
h.debug("Skip undefined instruction %d", h.programCounter()) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
h.debug("Instruction: %s", instruction.Instruction.Name) |
|
|
|
|
h.debug("Accumulator before: %f", h.accumulator) |
|
|
|
|
instruction.Execute(h) |
|
|
|
|
if err := instruction.Execute(h); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
h.debug("Accumulator after: %f", h.accumulator) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) Run() error { |
|
|
|
|
for !h.isStopped && h.programCounter() <= maxInstructions { |
|
|
|
|
h.Step() |
|
|
|
|
if err := h.Step(); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if h.programCounter() > maxInstructions { |
|
|
|
|
return fmt.Errorf("module exceeded max instructions without being stopped") |
|
|
|
|