Skip to content

Commit

Permalink
Add Presets (#66)
Browse files Browse the repository at this point in the history
* add presets, change Config*() funcs to variables, document
  • Loading branch information
bobertlo authored Nov 17, 2024
1 parent df807f0 commit f52880a
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 39 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ supplied as command line arguments. Both versions also accept the following
arguments

```
-preset string
Load named preset config (and ignore other flags)
-8 Enforce ICWS'88 rules
-F int
fixed position of warrior #2
Expand All @@ -57,6 +59,16 @@ arguments
Size of core (default 8000)
```

### Presets

| Name | Simulator Mode | CoreSize | Processes | Cycles | Length |
|---------|----------------|----------|-----------|--------|--------|
| nop94 | `NOP94` | 8000 | 8000 | 80000 | 100 |
| 88 | `ICWS88` | 8000 | 8000 | 80000 | 100 |
| icws | `ICWS88` | 8192 | 8000 | 100000 | 300 |
| noptiny | `NOP94` | 800 | 800 | 8000 | 20 |
| nopnano | `NOP94` | 80 | 80 | 800 | 5 |

### Visual MARS Controls

Keyboard controls:
Expand Down
30 changes: 20 additions & 10 deletions cmd/gmars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,30 @@ func main() {
roundFlag := flag.Int("r", 1, "Rounds to play")
debugFlag := flag.Bool("debug", false, "Dump verbose reporting of simulator state")
assembleFlag := flag.Bool("A", false, "Assemble and output warriors only")
presetFlag := flag.String("preset", "", "Load named preset config (and ignore other flags)")
flag.Parse()

coresize := gmars.Address(*sizeFlag)
processes := gmars.Address(*procFlag)
cycles := gmars.Address(*cycleFlag)
length := gmars.Address(*lenFlag)

var mode gmars.SimulatorMode
if *use88Flag {
mode = gmars.ICWS88
var config gmars.SimulatorConfig
if *presetFlag != "" {
presetConfig, err := gmars.PresetConfig(*presetFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading config: %s\n", err)
os.Exit(1)
}
config = presetConfig
} else {
mode = gmars.ICWS94
var mode gmars.SimulatorMode
if *use88Flag {
mode = gmars.ICWS88
} else {
mode = gmars.ICWS94
}
coresize := gmars.Address(*sizeFlag)
processes := gmars.Address(*procFlag)
cycles := gmars.Address(*cycleFlag)
length := gmars.Address(*lenFlag)
config = gmars.NewQuickConfig(mode, coresize, processes, cycles, length)
}
config := gmars.NewQuickConfig(mode, coresize, processes, cycles, length)

args := flag.Args()

Expand Down
30 changes: 20 additions & 10 deletions cmd/vmars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,30 @@ func main() {
// roundFlag := flag.Int("r", 1, "Rounds to play")
showReadFlag := flag.Bool("showread", false, "display reads in the visualizer")
debugFlag := flag.Bool("debug", false, "Dump verbose reporting of simulator state")
presetFlag := flag.String("preset", "", "Load named preset config (and ignore other flags)")
flag.Parse()

coresize := gmars.Address(*sizeFlag)
processes := gmars.Address(*procFlag)
cycles := gmars.Address(*cycleFlag)
length := gmars.Address(*lenFlag)

var mode gmars.SimulatorMode
if *use88Flag {
mode = gmars.ICWS88
var config gmars.SimulatorConfig
if *presetFlag != "" {
presetConfig, err := gmars.PresetConfig(*presetFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading config: %s\n", err)
os.Exit(1)
}
config = presetConfig
} else {
mode = gmars.ICWS94
var mode gmars.SimulatorMode
if *use88Flag {
mode = gmars.ICWS88
} else {
mode = gmars.ICWS94
}
coresize := gmars.Address(*sizeFlag)
processes := gmars.Address(*procFlag)
cycles := gmars.Address(*cycleFlag)
length := gmars.Address(*lenFlag)
config = gmars.NewQuickConfig(mode, coresize, processes, cycles, length)
}
config := gmars.NewQuickConfig(mode, coresize, processes, cycles, length)

args := flag.Args()

Expand Down
10 changes: 5 additions & 5 deletions compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func runWarriorLoadFileTests(t *testing.T, tests []warriorTestCase) {
}

func TestCompileWarriors88(t *testing.T) {
config := ConfigKOTH88()
config := ConfigKOTH88
tests := []warriorTestCase{
{
filename: "warriors/88/imp.red",
Expand All @@ -76,7 +76,7 @@ func TestCompileWarriors88(t *testing.T) {
}

func TestCompileWarriors94(t *testing.T) {
config := ConfigNOP94()
config := ConfigNOP94
tests := []warriorTestCase{
{
filename: "warriors/94/imp.red",
Expand All @@ -96,7 +96,7 @@ func TestCompileWarriors94(t *testing.T) {
}

func TestCompileWarriorsFile94(t *testing.T) {
config := ConfigNOP94()
config := ConfigNOP94
tests := []warriorTestCase{
{
filename: "warriors/94/simpleshot.red",
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestCompileWarriorsFile94(t *testing.T) {
}

func TestCompileForLoop(t *testing.T) {
config := ConfigNOP94()
config := ConfigNOP94

input := `
dat 123, 123
Expand All @@ -146,7 +146,7 @@ func TestCompileForLoop(t *testing.T) {
}

func TestCompileDoubleForLoop(t *testing.T) {
config := ConfigNOP94()
config := ConfigNOP94

input := `
org start
Expand Down
55 changes: 49 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type SimulatorConfig struct {
Distance Address
}

func ConfigKOTH88() SimulatorConfig {
return SimulatorConfig{
var (
ConfigKOTH88 = SimulatorConfig{
Mode: ICWS88,
CoreSize: 8000,
Processes: 8000,
Expand All @@ -24,10 +24,17 @@ func ConfigKOTH88() SimulatorConfig {
Length: 100,
Distance: 100,
}
}

func ConfigNOP94() SimulatorConfig {
return SimulatorConfig{
ConfigICWS88 = SimulatorConfig{
Mode: ICWS88,
CoreSize: 8192,
Processes: 8000,
Cycles: 10000,
ReadLimit: 8000,
WriteLimit: 8000,
Length: 300,
Distance: 100,
}
ConfigNOP94 = SimulatorConfig{
Mode: ICWS94,
CoreSize: 8000,
Processes: 8000,
Expand All @@ -37,6 +44,42 @@ func ConfigNOP94() SimulatorConfig {
Length: 100,
Distance: 100,
}
ConfigNopTiny = SimulatorConfig{
Mode: NOP94,
CoreSize: 800,
Processes: 800,
Cycles: 8000,
ReadLimit: 800,
WriteLimit: 800,
Length: 20,
Distance: 20,
}
ConfigNopNano = SimulatorConfig{
Mode: NOP94,
CoreSize: 80,
Processes: 80,
Cycles: 800,
ReadLimit: 80,
WriteLimit: 80,
Length: 5,
Distance: 5,
}

configPresets = map[string]SimulatorConfig{
"88": ConfigKOTH88,
"icws": ConfigICWS88,
"nop94": ConfigNOP94,
"noptiny": ConfigNopTiny,
"nopnano": ConfigNopNano,
}
)

func PresetConfig(name string) (SimulatorConfig, error) {
config, ok := configPresets[name]
if !ok {
return SimulatorConfig{}, fmt.Errorf("preset '%s' not found", name)
}
return config, nil
}

func NewQuickConfig(mode SimulatorMode, coreSize, processes, cycles, length Address) SimulatorConfig {
Expand Down
12 changes: 6 additions & 6 deletions load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type instructionParseTestCase struct {
}

func TestLoadImp88(t *testing.T) {
config := ConfigKOTH88()
config := ConfigKOTH88

reader := bytes.NewReader(imp_88_red)
data, err := ParseLoadFile(reader, config)
Expand All @@ -30,7 +30,7 @@ func TestLoadImp88(t *testing.T) {
}

func TestLoadImp94(t *testing.T) {
config := ConfigNOP94()
config := ConfigNOP94

reader := bytes.NewReader(imp_94_red)
data, err := ParseLoadFile(reader, config)
Expand All @@ -44,7 +44,7 @@ func TestLoadImp94(t *testing.T) {
}

func TestLoadDwarf(t *testing.T) {
config := ConfigKOTH88()
config := ConfigKOTH88

dwarf_code := `
ADD # 4, $ 3
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestValidInput(t *testing.T) {
"\n\n",
}

config := ConfigKOTH88()
config := ConfigKOTH88
for i, testCase := range cases {
reader := strings.NewReader(testCase)
_, err := ParseLoadFile(reader, config)
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestInvalidInput(t *testing.T) {
"MOV $ 0, $ ooops\n",
}

config := ConfigKOTH88()
config := ConfigKOTH88

for i, testCase := range cases {
reader := strings.NewReader(testCase)
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestValidOpModeCombos88(t *testing.T) {
{"JMP < 1, < 2\n", Instruction{Op: JMP, OpMode: B, AMode: B_DECREMENT, A: 1, BMode: B_DECREMENT, B: 2}},
}

config := ConfigKOTH88()
config := ConfigKOTH88

for i, testCase := range testCases {
reader := strings.NewReader(testCase.input)
Expand Down
4 changes: 2 additions & 2 deletions sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestSimDwarf(t *testing.T) {

func TestRunImp(t *testing.T) {
reader := bytes.NewReader(imp_88_red)
config := ConfigKOTH88()
config := ConfigKOTH88
impdata, err := ParseLoadFile(reader, config)
require.NoError(t, err)

Expand All @@ -67,7 +67,7 @@ func TestRunImp(t *testing.T) {

func TestRunTwoImps(t *testing.T) {
reader := bytes.NewReader(imp_88_red)
config := ConfigKOTH88()
config := ConfigKOTH88
impdata, err := ParseLoadFile(reader, config)
require.NoError(t, err)

Expand Down

0 comments on commit f52880a

Please sign in to comment.