diff --git a/cmd/root.go b/cmd/root.go index 846886c..8167a53 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -52,6 +52,9 @@ func runRootCommand(cmd *cobra.Command, args []string) error { if err != nil { return err } - hal.NewHALModule(program, 256, debug) - return nil + module, err := hal.NewHALModule(program, 256, 2, debug) + if err != nil { + return err + } + return module.Run() } diff --git a/hal/instructions.go b/hal/instructions.go index 043c272..7e64679 100644 --- a/hal/instructions.go +++ b/hal/instructions.go @@ -59,8 +59,8 @@ var InstructionStop = &Instruction{ var InstructionOut = &Instruction{ Name: "OUT", - Execute: func(module *Module) { - // TODO implement + ExecuteWithOperand: func(module *Module, operand float64) { + module.IO[int64(operand)] = module.accumulator }, } diff --git a/hal/module.go b/hal/module.go index d4b4779..8f33a12 100644 --- a/hal/module.go +++ b/hal/module.go @@ -7,20 +7,21 @@ const maxInstructions = 1000 type Program map[int64]*ProgrammedInstruction type Module struct { - Accumulator float64 - ProgramStorage Program - Register []float64 + accumulator float64 + programStorage Program + register []float64 + IO []float64 isStopped bool debugEnabled bool } func (h *Module) ProgramCounter() int64 { - return int64(h.Register[0]) + return int64(h.register[0]) } func (h *Module) increaseProgramCounter() { - h.Register[0]++ + h.register[0]++ } func (h *Module) debug(format string, args ...interface{}) { @@ -31,15 +32,15 @@ func (h *Module) debug(format string, args ...interface{}) { } func (h *Module) Step() { - instruction := h.ProgramStorage[h.ProgramCounter()] + 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) + h.debug("Accumulator before: %d", h.accumulator) instruction.Execute(h) - h.debug("Accumulator after: %d", h.Accumulator) + h.debug("Accumulator after: %d", h.accumulator) } func (h *Module) Run() error { @@ -52,13 +53,14 @@ func (h *Module) Run() error { 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 { return nil, fmt.Errorf("register size must be greater then 10 [ registerSize = %d ]", registerSize) } return &Module{ - ProgramStorage: program, - Register: make([]float64, registerSize), + programStorage: program, + register: make([]float64, registerSize), + IO: make([]float64, ioSize), debugEnabled: debug, }, nil }