main.go (3992B)
1 package main 2 3 import ( 4 "bufio" 5 "flag" 6 "fmt" 7 "io" 8 "log" 9 "os" 10 11 "github.com/gboncoffee/egg/assembler" 12 "github.com/gboncoffee/egg/machine" 13 "github.com/gboncoffee/egg/mips" 14 "github.com/gboncoffee/egg/pia" 15 "github.com/gboncoffee/egg/reduxK" 16 "github.com/gboncoffee/egg/reduxPia" 17 "github.com/gboncoffee/egg/reduxv" 18 "github.com/gboncoffee/egg/riscv" 19 "github.com/gboncoffee/egg/sagui" 20 ) 21 22 const VERSION = "3.5.3" 23 24 // Put new architetures here... (main.go:/switch architeture) 25 func listArchs() { 26 fmt.Println(machine.InterCtx.Get(`Currently supported architetures: 27 'riscv' - RISC-V IM, 32 bits 28 'mips' - Subset of MIPS32 29 'sagui' - Fantasy 8 bit RISC 30 'reduxv' - 8 bit version of RISC-V 31 'reduxK' - Kaluf's version of reduxv 32 'reduxPia - Gabriel's version of reduxv (REDUX-PIÁ) 33 'pia' - Processador de Informação Avançado (experimental)`)) 34 } 35 36 func version() { 37 fmt.Println(machine.InterCtx.Get("EGG - Emulador Genérico do Gabriel - version ") + VERSION) 38 } 39 40 func runMachine(m machine.Machine) { 41 for { 42 call, err := m.NextInstruction() 43 if err != nil { 44 log.Printf(machine.InterCtx.Get("Instruction execution failed: %v\n"), err) 45 return 46 } 47 48 if call != nil { 49 switch call.Number { 50 case machine.SYS_BREAK: 51 return 52 case machine.SYS_READ: 53 // Lol I'm using 3 std modules just to read from stdin. 54 addr := call.Arg1 55 size := call.Arg2 56 buf := make([]uint8, size) 57 reader := bufio.NewReader(os.Stdin) 58 _, _ = io.ReadFull(reader, buf) 59 _ = m.SetMemoryChunk(addr, buf) 60 case machine.SYS_WRITE: 61 addr := call.Arg1 62 size := call.Arg2 63 buf, _ := m.GetMemoryChunk(addr, size) 64 fmt.Print(string(buf)) 65 } 66 } 67 } 68 } 69 70 func main() { 71 var architeture string 72 var debug bool 73 var list bool 74 var ver bool 75 var m machine.Machine 76 77 machine.InterCtx.Init() 78 _ = machine.InterCtx.AddLocale("pt_BR", brazilian) 79 _ = machine.InterCtx.AutoSetPreferedLocale() 80 assembler.InterCtx = &machine.InterCtx 81 82 log.SetFlags(0) 83 84 flag.StringVar(&architeture, "arch", "riscv", machine.InterCtx.Get("Select architeture to use.")) 85 flag.StringVar(&architeture, "a", "riscv", machine.InterCtx.Get("Select architeture to use (shorthand).")) 86 flag.BoolVar(&list, "list-archs", false, machine.InterCtx.Get("Lists currently supported architetures and quit.")) 87 flag.BoolVar(&list, "l", false, machine.InterCtx.Get("Lists currently supported architetures (shorthand).")) 88 flag.BoolVar(&ver, "version", false, machine.InterCtx.Get("Show current version and quit.")) 89 flag.BoolVar(&ver, "v", false, machine.InterCtx.Get("Show current version and quit (shorthand).")) 90 flag.BoolVar(&debug, "debug", false, machine.InterCtx.Get("Enter debugger upon startup.")) 91 flag.BoolVar(&debug, "d", false, machine.InterCtx.Get("Enter debugger upon startup (shorthand).")) 92 93 flag.Parse() 94 95 if list { 96 version() 97 listArchs() 98 return 99 } 100 101 if ver { 102 version() 103 return 104 } 105 106 // ...and in the switch case! (main.go:/func listArchs) 107 switch architeture { 108 case "riscv": 109 var r riscv.RiscV 110 m = &r 111 case "mips": 112 var r mips.Mips 113 m = &r 114 case "sagui": 115 var r sagui.Sagui 116 m = &r 117 case "reduxv": 118 m = reduxv.ReduxV() 119 case "reduxK": 120 m = reduxK.ReduxK() 121 case "reduxPia": 122 m = reduxPia.ReduxPia() 123 case "pia": 124 var r pia.Pia 125 m = &r 126 default: 127 log.Printf(machine.InterCtx.Get("Unknown architeture: %v\n"), architeture) 128 listArchs() 129 os.Exit(1) 130 } 131 132 file := flag.Arg(0) 133 if file == "" { 134 log.Println(machine.InterCtx.Get("No Assembly file supplied.")) 135 os.Exit(1) 136 } 137 138 code, sym, err := m.Assemble(file) 139 if err != nil { 140 log.Println(err) 141 os.Exit(1) 142 } 143 144 err = m.LoadProgram(code) 145 if err != nil { 146 log.Printf(machine.InterCtx.Get("Error loading assembled program: %v\n"), err) 147 os.Exit(1) 148 } 149 150 if debug { 151 if sym == nil { 152 log.Println(machine.InterCtx.Get("Debugging is not supported for the selected backend.")) 153 os.Exit(1) 154 } 155 // Hello fellow Acme user. Plumb this: debugger.go:/debugMachine 156 debugMachine(m, sym, code, file) 157 } else { 158 runMachine(m) 159 } 160 }