commit 08c164fdbb2b7592e91e9b3123761f0a0b243dc3
parent 4b5a14cb81e45c6b7bf877d062895a1e042f44bf
Author: Gabriel G. de Brito <gabrielgbrito@icloud.com>
Date: Fri, 15 Dec 2023 10:24:44 -0300
Setup debugger and runner initialization
Diffstat:
| M | machine/machine.go | | | 28 | ++++++++++++++++++++++++++-- |
| M | main.go | | | 62 | +++++++++++++++++++++++++++++++++++++++++++++++++------------- |
2 files changed, 75 insertions(+), 15 deletions(-)
diff --git a/machine/machine.go b/machine/machine.go
@@ -1,4 +1,4 @@
-// Package egg/machine has the interface used by EGG backends.
+// Package egg/machine has the interface and syscall struct used by EGG backends.
package machine
// Interface that machine structs are required to implement to work with EGG.
@@ -8,7 +8,7 @@ package machine
// bits can be done with a single x86 instruction.
type Machine interface {
LoadProgram([]uint8) error
- NextInstruction() error
+ NextInstruction() (*Call, error)
GetMemory(uint64) (uint8, error)
SetMemory(uint64, uint8) error
// Address than size.
@@ -18,3 +18,27 @@ type Machine interface {
// Address than content.
SetRegister(uint64, uint64) error
}
+
+// Syscalls numbers. ISAs with specific calls for BREAK should send a BREAK on them.
+//
+// BREAK - 1 - Transfer control to debugger or stop machine.
+// - No arguments.
+//
+// READ - 2 - Read input.
+// - Arg1: Buffer address (will be put into SetMemoryChunk()).
+// - Arg2: Size in bytes of input.
+//
+// WRITE - 3 - Write output.
+// - Arg1: Buffer address (will be put into GetMemoryChunk()).
+// - Arg2: Size in bytes of output.
+const (
+ SYS_BREAK = 1
+ SYS_READ = 2
+ SYS_WRITE = 3
+)
+
+type Call struct {
+ Number uint64
+ Arg1 uint64
+ Arg2 uint64
+}
diff --git a/main.go b/main.go
@@ -2,46 +2,82 @@ package main
import (
"flag"
+ "log"
+ "fmt"
+ "os"
"github.com/gboncoffee/egg/riscv"
"github.com/gboncoffee/egg/machine"
)
-func runMachine(m machine.Machine) {
- panic("not yet implemented")
+// Put new architetures here...
+func listArchs() {
+ fmt.Println(`Currently supported architetures:
+'riscv' - RISC-V IM, 32 bits`)
+}
+
+func version() {
+ fmt.Println("EGG - Emulador Genérico do Gabriel - version 0.0.1rc")
}
-func debugMachine(m machine.Machine) {
- panic("not yet implemented")
+func runMachine(m machine.Machine) {
+ for {
+ call, err := m.NextInstruction()
+ if err != nil {
+ log.Println(fmt.Sprintf("Instruction execution failed: %v", err))
+ return
+ }
+
+ if call != nil {
+ if call.Number == machine.SYS_BREAK {
+ return
+ }
+ }
+ }
}
func main() {
var architeture string
var debug bool
- var file string
- var outf string
+ var list bool
var m machine.Machine
- // Add new architetures to the help string!
- flag.StringVar(&architeture, "arch", "riscv", "Select architeture to use. Currently available: 'riscv'.")
+ log.SetFlags(0)
+
+ flag.StringVar(&architeture, "arch", "riscv", "Select architeture to use.")
flag.StringVar(&architeture, "a", "riscv", "Select architeture to use (shorthand).")
+ flag.BoolVar(&list, "list-archs", false, "Lists currently supported architetures and quit.")
+ flag.BoolVar(&list, "l", false, "Lists currently supported architetures (shorthand).")
flag.BoolVar(&debug, "debug", false, "Enter debugger upon startup.")
flag.BoolVar(&debug, "d", false, "Enter debugger upon startup (shorthand).")
- flag.StringVar(&file, "file", "", "Assembly source or ELF file to load.")
- flag.StringVar(&file, "f", "", "Assembly source or ELF file to load (shorthand).")
- flag.StringVar(&outf, "out", "", "ELF file to save assembled code.")
- flag.StringVar(&outf, "o", "", "ELF file to save assembled code (shorthand).")
flag.Parse()
- // And the switch case!
+ if list {
+ version()
+ listArchs()
+ return
+ }
+
+ // ...and in the switch case!
switch architeture {
case "riscv":
var r riscv.RiscV
m = &r
+ default:
+ log.Println(fmt.Sprintf("Unknown architeture: %v", architeture))
+ listArchs()
+ os.Exit(1)
+ }
+
+ file := flag.Arg(0)
+ if file == "" {
+ log.Println("No Assembly file supplied.")
+ os.Exit(1)
}
if debug {
+ // Hello fellow Acme user. Plumb this: debugger.go:/debugMachine
debugMachine(m)
} else {
runMachine(m)