diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index ec01c6d..8167a53 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,8 +24,10 @@ var rootCommand = &cobra.Command{ RunE: runRootCommand, } +var debug bool + func init() { - rootCommand.Flags().BoolP("debug", "d", false, "Enable debug mode") + rootCommand.Flags().BoolVarP(&debug, "debug", "d", false, "Enable debug mode") } func runRootCommand(cmd *cobra.Command, args []string) error { @@ -50,7 +52,7 @@ func runRootCommand(cmd *cobra.Command, args []string) error { if err != nil { return err } - module, err := hal.NewHALModule(program, 256, 2) + module, err := hal.NewHALModule(program, 256, 2, debug) if err != nil { return err } diff --git a/hal/instructions.go b/hal/instructions.go index 4aa198e..c637348 100644 --- a/hal/instructions.go +++ b/hal/instructions.go @@ -12,6 +12,7 @@ type ProgrammedInstruction struct { func (pi *ProgrammedInstruction) Execute(module *Module) error { if pi.Instruction.ExecuteWithOperand != nil { + module.debug("Operand: %d", pi.Operand) return pi.Instruction.ExecuteWithOperand(module, pi.Operand) } else if pi.Instruction.Execute != nil { return pi.Instruction.Execute(module) @@ -40,6 +41,7 @@ var instructions = []*Instruction{ InstructionStop, InstructionIn, InstructionOut, + InstructionAddnum, } var InstructionStart = &Instruction{ @@ -128,3 +130,10 @@ var InstructionJumpNeg = &Instruction{ return nil }, } + +var InstructionAddnum = &Instruction{ + Name: "ADDNUM", + ExecuteWithOperand: func(module *Module, operand float64) { + module.accumulator = module.accumulator + operand + }, +} diff --git a/hal/module.go b/hal/module.go index f8597d4..fbf4532 100644 --- a/hal/module.go +++ b/hal/module.go @@ -11,7 +11,9 @@ type Module struct { programStorage Program register []float64 IO []float64 - isStopped bool + + 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 }