|
|
|
@ -10,7 +10,9 @@ type Module struct { |
|
|
|
|
Accumulator float64 |
|
|
|
|
ProgramStorage Program |
|
|
|
|
Register []float64 |
|
|
|
|
isStopped bool |
|
|
|
|
|
|
|
|
|
isStopped bool |
|
|
|
|
debugEnabled bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) ProgramCounter() int64 { |
|
|
|
@ -21,13 +23,23 @@ func (h *Module) increaseProgramCounter() { |
|
|
|
|
h.Register[0]++ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) debug(format string, args ...interface{}) { |
|
|
|
|
if !h.debugEnabled { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
fmt.Printf("[DEBUG] "+format, args...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) Step() { |
|
|
|
|
instruction := h.ProgramStorage[h.ProgramCounter()] |
|
|
|
|
h.increaseProgramCounter() |
|
|
|
|
if instruction == nil { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
h.debug("Instruction: %s", instruction.Instruction.Name) |
|
|
|
|
h.debug("Accumulator before: %d", h.Accumulator) |
|
|
|
|
instruction.Execute(h) |
|
|
|
|
h.debug("Accumulator after: %d", h.Accumulator) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) Run() error { |
|
|
|
@ -40,12 +52,13 @@ func (h *Module) Run() error { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewHALModule(program Program, registerSize uint64) (*Module, error) { |
|
|
|
|
func NewHALModule(program Program, registerSize uint64, debug bool) (*Module, error) { |
|
|
|
|
if registerSize <= 10 { |
|
|
|
|
return nil, fmt.Errorf("register size must be greater then 10 [ registerSize = %d ]", registerSize) |
|
|
|
|
} |
|
|
|
|
return &Module{ |
|
|
|
|
ProgramStorage: program, |
|
|
|
|
Register: make([]float64, registerSize), |
|
|
|
|
debugEnabled: debug, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|