Merge remote-tracking branch 'origin/master'

# Conflicts:
#	go.sum
#	hal/instructions.go
master
Jonas Franz 5 years ago
commit 2e5f40e271
Signed by: JonasFranzDEV
GPG Key ID: 7293A220B7C38080
  1. 1
      .gitignore
  2. 6
      cmd/root.go
  3. 9
      hal/instructions.go
  4. 17
      hal/module.go

1
.gitignore vendored

@ -0,0 +1 @@
.idea/

@ -24,8 +24,10 @@ var rootCommand = &cobra.Command{
RunE: runRootCommand, RunE: runRootCommand,
} }
var debug bool
func init() { 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 { func runRootCommand(cmd *cobra.Command, args []string) error {
@ -50,7 +52,7 @@ func runRootCommand(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
module, err := hal.NewHALModule(program, 256, 2) module, err := hal.NewHALModule(program, 256, 2, debug)
if err != nil { if err != nil {
return err return err
} }

@ -12,6 +12,7 @@ type ProgrammedInstruction struct {
func (pi *ProgrammedInstruction) Execute(module *Module) error { func (pi *ProgrammedInstruction) Execute(module *Module) error {
if pi.Instruction.ExecuteWithOperand != nil { if pi.Instruction.ExecuteWithOperand != nil {
module.debug("Operand: %d", pi.Operand)
return pi.Instruction.ExecuteWithOperand(module, pi.Operand) return pi.Instruction.ExecuteWithOperand(module, pi.Operand)
} else if pi.Instruction.Execute != nil { } else if pi.Instruction.Execute != nil {
return pi.Instruction.Execute(module) return pi.Instruction.Execute(module)
@ -40,6 +41,7 @@ var instructions = []*Instruction{
InstructionStop, InstructionStop,
InstructionIn, InstructionIn,
InstructionOut, InstructionOut,
InstructionAddnum,
} }
var InstructionStart = &Instruction{ var InstructionStart = &Instruction{
@ -128,3 +130,10 @@ var InstructionJumpNeg = &Instruction{
return nil return nil
}, },
} }
var InstructionAddnum = &Instruction{
Name: "ADDNUM",
ExecuteWithOperand: func(module *Module, operand float64) {
module.accumulator = module.accumulator + operand
},
}

@ -11,7 +11,9 @@ type Module struct {
programStorage Program programStorage Program
register []float64 register []float64
IO []float64 IO []float64
isStopped bool
isStopped bool
debugEnabled bool
} }
func (h *Module) programCounter() int64 { func (h *Module) programCounter() int64 {
@ -29,13 +31,23 @@ func (h *Module) increaseProgramCounter() {
h.register[0]++ 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() { 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("Accumulator before: %f", h.accumulator)
instruction.Execute(h) instruction.Execute(h)
h.debug("Accumulator after: %f", h.accumulator)
} }
func (h *Module) Run() error { func (h *Module) Run() error {
@ -48,7 +60,7 @@ func (h *Module) Run() error {
return nil 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 { 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)
} }
@ -56,5 +68,6 @@ func NewHALModule(program Program, registerSize uint64, ioSize uint64) (*Module,
programStorage: program, programStorage: program,
register: make([]float64, registerSize), register: make([]float64, registerSize),
IO: make([]float64, ioSize), IO: make([]float64, ioSize),
debugEnabled: debug,
}, nil }, nil
} }

Loading…
Cancel
Save