|
|
|
@ -11,7 +11,9 @@ type Module struct { |
|
|
|
|
programStorage Program |
|
|
|
|
register []float64 |
|
|
|
|
IO []float64 |
|
|
|
|
|
|
|
|
|
isStopped bool |
|
|
|
|
debugEnabled bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) programCounter() int64 { |
|
|
|
@ -29,13 +31,23 @@ func (h *Module) increaseProgramCounter() { |
|
|
|
|
h.register[0]++ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) debug(format string, args ...interface{}) { |
|
|
|
|
if !h.debugEnabled { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
fmt.Printf("[DEBUG] "+format+"\n", 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: %f", h.accumulator) |
|
|
|
|
instruction.Execute(h) |
|
|
|
|
h.debug("Accumulator after: %f", h.accumulator) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Module) Run() error { |
|
|
|
@ -48,7 +60,7 @@ func (h *Module) Run() error { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewHALModule(program Program, registerSize uint64, ioSize uint64) (*Module, error) { |
|
|
|
|
func NewHALModule(program Program, registerSize uint64, ioSize uint64, debug bool) (*Module, error) { |
|
|
|
|
if registerSize <= 10 { |
|
|
|
|
return nil, fmt.Errorf("register size must be greater then 10 [ registerSize = %d ]", registerSize) |
|
|
|
|
} |
|
|
|
@ -56,5 +68,6 @@ func NewHALModule(program Program, registerSize uint64, ioSize uint64) (*Module, |
|
|
|
|
programStorage: program, |
|
|
|
|
register: make([]float64, registerSize), |
|
|
|
|
IO: make([]float64, ioSize), |
|
|
|
|
debugEnabled: debug, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|