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. 15
      hal/module.go

1
.gitignore vendored

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

@ -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
}

@ -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
},
}

@ -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
}

Loading…
Cancel
Save