|
|
@ -7,20 +7,21 @@ const maxInstructions = 1000 |
|
|
|
type Program map[int64]*ProgrammedInstruction |
|
|
|
type Program map[int64]*ProgrammedInstruction |
|
|
|
|
|
|
|
|
|
|
|
type Module struct { |
|
|
|
type Module struct { |
|
|
|
Accumulator float64 |
|
|
|
accumulator float64 |
|
|
|
ProgramStorage Program |
|
|
|
programStorage Program |
|
|
|
Register []float64 |
|
|
|
register []float64 |
|
|
|
|
|
|
|
IO []float64 |
|
|
|
|
|
|
|
|
|
|
|
isStopped bool |
|
|
|
isStopped bool |
|
|
|
debugEnabled bool |
|
|
|
debugEnabled bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (h *Module) ProgramCounter() int64 { |
|
|
|
func (h *Module) ProgramCounter() int64 { |
|
|
|
return int64(h.Register[0]) |
|
|
|
return int64(h.register[0]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (h *Module) increaseProgramCounter() { |
|
|
|
func (h *Module) increaseProgramCounter() { |
|
|
|
h.Register[0]++ |
|
|
|
h.register[0]++ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (h *Module) debug(format string, args ...interface{}) { |
|
|
|
func (h *Module) debug(format string, args ...interface{}) { |
|
|
@ -31,15 +32,15 @@ func (h *Module) debug(format string, args ...interface{}) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (h *Module) Step() { |
|
|
|
func (h *Module) Step() { |
|
|
|
instruction := h.ProgramStorage[h.ProgramCounter()] |
|
|
|
instruction := h.programStorage[h.ProgramCounter()] |
|
|
|
h.increaseProgramCounter() |
|
|
|
h.increaseProgramCounter() |
|
|
|
if instruction == nil { |
|
|
|
if instruction == nil { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
h.debug("Instruction: %s", instruction.Instruction.Name) |
|
|
|
h.debug("Instruction: %s", instruction.Instruction.Name) |
|
|
|
h.debug("Accumulator before: %d", h.Accumulator) |
|
|
|
h.debug("Accumulator before: %d", h.accumulator) |
|
|
|
instruction.Execute(h) |
|
|
|
instruction.Execute(h) |
|
|
|
h.debug("Accumulator after: %d", h.Accumulator) |
|
|
|
h.debug("Accumulator after: %d", h.accumulator) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (h *Module) Run() error { |
|
|
|
func (h *Module) Run() error { |
|
|
@ -52,13 +53,14 @@ func (h *Module) Run() error { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewHALModule(program Program, registerSize uint64, debug bool) (*Module, error) { |
|
|
|
func NewHALModule(program Program, registerSize uint64, ioSize uint64, debug bool) (*Module, error) { |
|
|
|
if registerSize <= 10 { |
|
|
|
if registerSize <= 10 { |
|
|
|
return nil, fmt.Errorf("register size must be greater then 10 [ registerSize = %d ]", registerSize) |
|
|
|
return nil, fmt.Errorf("register size must be greater then 10 [ registerSize = %d ]", registerSize) |
|
|
|
} |
|
|
|
} |
|
|
|
return &Module{ |
|
|
|
return &Module{ |
|
|
|
ProgramStorage: program, |
|
|
|
programStorage: program, |
|
|
|
Register: make([]float64, registerSize), |
|
|
|
register: make([]float64, registerSize), |
|
|
|
|
|
|
|
IO: make([]float64, ioSize), |
|
|
|
debugEnabled: debug, |
|
|
|
debugEnabled: debug, |
|
|
|
}, nil |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|