|
|
|
@ -2,12 +2,15 @@ package hal |
|
|
|
|
|
|
|
|
|
import "fmt" |
|
|
|
|
|
|
|
|
|
const maxInstructions = 1000 |
|
|
|
|
|
|
|
|
|
type Program map[int64]*ProgrammedInstruction |
|
|
|
|
|
|
|
|
|
type Module struct { |
|
|
|
|
Accumulator float64 |
|
|
|
|
ProgramStorage Program |
|
|
|
|
Register []float64 |
|
|
|
|
isStopped bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) ProgramCounter() int64 { |
|
|
|
@ -18,13 +21,23 @@ func (h *Module) increaseProgramCounter() { |
|
|
|
|
h.Register[0]++ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) Step() error { |
|
|
|
|
func (h *Module) Step() { |
|
|
|
|
instruction := h.ProgramStorage[h.ProgramCounter()] |
|
|
|
|
h.increaseProgramCounter() |
|
|
|
|
if instruction == nil { |
|
|
|
|
return fmt.Errorf("instruction not found for program counter = %d", h.ProgramCounter()) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
instruction.Execute(h) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) Run() error { |
|
|
|
|
for !h.isStopped && h.ProgramCounter() <= maxInstructions { |
|
|
|
|
h.Step() |
|
|
|
|
} |
|
|
|
|
if h.ProgramCounter() > maxInstructions { |
|
|
|
|
return fmt.Errorf("module exceeded max instructions without being stopped") |
|
|
|
|
} |
|
|
|
|
return instruction.Execute(h) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewHALModule(program Program, registerSize uint64) (*Module, error) { |
|
|
|
|