Documentation
¶
Overview ¶
Package mars implements interfaces for MARS simulators and helper functions to define configuration and load warrior files.
The Simulator interface provides a MARS implemenation for running simulations, and the ReportingSimulator interface adds the Addreporter method to inject Reporter interfaces to receive callbacks to report state changes in the simulation.
RedCode files are first loaded as WarriorData{} structs, holding the data needed to create a Warrior inside a Simulator. These can safely be reused to create multiple Simulators concurrently.
The SimulatorConfig struct is provided to define configuration when creating Simulator instances, and compiling warriors.
Index ¶
- Variables
- func ExpandAndEvaluate(expr []token, symbols map[string][]token) (int, error)
- func ForExpand(lex tokenReader, symbols map[string][]token) ([]token, error)
- func LexInput(r io.Reader) ([]token, error)
- func ScanInput(lex tokenReader) (map[string][]token, bool, error)
- type Address
- type AddressMode
- type CoreState
- type Instruction
- type OpCode
- type OpMode
- type Report
- type ReportType
- type Reporter
- type ReportingSimulator
- type Simulator
- type SimulatorConfig
- type SimulatorMode
- type SimulatorState
- type StateRecorder
- type Warrior
- type WarriorData
- type WarriorState
Constants ¶
This section is empty.
Variables ¶
var ( ConfigKOTH88 = SimulatorConfig{ Mode: ICWS88, CoreSize: 8000, Processes: 8000, Cycles: 80000, ReadLimit: 8000, WriteLimit: 8000, Length: 100, Distance: 100, } 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, Cycles: 80000, ReadLimit: 8000, WriteLimit: 8000, Length: 100, Distance: 100, } ConfigNopTiny = SimulatorConfig{ Mode: NOP94, CoreSize: 800, Processes: 800, Cycles: 8000, ReadLimit: 800, WriteLimit: 800, Length: 20, Distance: 20, } ConfigNop256 = SimulatorConfig{ Mode: NOP94, CoreSize: 256, Processes: 60, Cycles: 2560, ReadLimit: 800, WriteLimit: 800, Length: 10, Distance: 10, } ConfigNopNano = SimulatorConfig{ Mode: NOP94, CoreSize: 80, Processes: 80, Cycles: 800, ReadLimit: 80, WriteLimit: 80, Length: 5, Distance: 5, } )
var ( //go:embed warriors/88/imp.red Imp_88_red []byte //go:embed warriors/94/imp.red Imp_94_red []byte //go:embed warriors/94/simpleshot.red SimpleShot_94_red []byte //go:embed warriors/94/bombspiral.red BombSpiral_94_red []byte )
Functions ¶
func ExpandAndEvaluate ¶ added in v0.1.14
Types ¶
type AddressMode ¶
type AddressMode uint8
const ( DIRECT AddressMode = iota // "$" direct reference to another address IMMEDIATE // "#" use the immediate value of this instruction A_INDIRECT // "*" use the A-Field of the address referenced by a pointer B_INDIRECT // "@" use the B-Field of the address referenced by a pointer A_DECREMENT // "{" use the A-field of the address referenced by a pointer, after decrementing B_DECREMENT // "<" use the B-field of the address referenced by a pointer, after decrementing A_INCREMENT // "}" use the A-field of the address referenced by a pointer, before incrementing B_INCREMENT // ">" use the B-field of the address referenced by a pointer, before incrementing )
func (AddressMode) String ¶
func (m AddressMode) String() string
String returns the single character string representation of an AddressMode, or "?"
type Instruction ¶
type Instruction struct { Op OpCode OpMode OpMode A Address AMode AddressMode B Address BMode AddressMode }
Instruction represents the raw values of a memory address
func (Instruction) NormString ¶
func (i Instruction) NormString(coresize Address) string
NormString returns the decompiled instruction with signed field values normalized to a core size.
func (Instruction) String ¶
func (i Instruction) String() string
String returns the decompiled instruction with unsigned field values
type ReportType ¶
type ReportType uint8
const ( SimReset ReportType = iota CycleStart CycleEnd WarriorSpawn WarriorTaskPop WarriorTaskPush WarriorTaskTerminate WarriorTerminate WarriorRead WarriorWrite WarriorDecrement WarriorIncrement )
type ReportingSimulator ¶
func NewReportingSimulator ¶
func NewReportingSimulator(config SimulatorConfig) (ReportingSimulator, error)
type Simulator ¶
type Simulator interface { CoreSize() Address CycleCount() int MaxCycles() int AddWarrior(data *WarriorData) (Warrior, error) GetWarrior(wi int) Warrior SpawnWarrior(wi int, startOffset Address) error Run() []bool // RunCycle runs a full cyle of the living warriors, starting at s,warriorIndex. // // If s.cycleCount > s.maxCycles or there are no living warriors, no warrior // will be run, s.cycleCount will not be incremented, and the return value will // be 0. Otherwise s.cycleCount is incremented and RunCycle() int GetMem(a Address) Instruction Reset() WarriorLivingCount() int WarriorCount() int }
func NewSimulator ¶
func NewSimulator(config SimulatorConfig) (Simulator, error)
type SimulatorConfig ¶
type SimulatorConfig struct { Mode SimulatorMode CoreSize Address Processes Address Cycles Address ReadLimit Address WriteLimit Address Length Address Distance Address }
func NewQuickConfig ¶
func NewQuickConfig(mode SimulatorMode, coreSize, processes, cycles, length Address) SimulatorConfig
func PresetConfig ¶ added in v0.1.11
func PresetConfig(name string) (SimulatorConfig, error)
func (SimulatorConfig) Validate ¶
func (c SimulatorConfig) Validate() error
type SimulatorState ¶
type SimulatorState uint8
const ( Initialized SimulatorState = iota Running Complete )
type StateRecorder ¶
type StateRecorder struct {
// contains filtered or unexported fields
}
StateRecorder implements a Reporter which records the most recent operation performed each core address and the warrior index associated. The initial state of each address is CoreEmpty with a warrior index of -1.
func NewStateRecorder ¶
func NewStateRecorder(sim ReportingSimulator) *StateRecorder
func (*StateRecorder) GetMemState ¶
func (r *StateRecorder) GetMemState(a Address) (CoreState, int)
func (*StateRecorder) Report ¶
func (r *StateRecorder) Report(report Report)
func (*StateRecorder) SetRecordRead ¶ added in v0.1.9
func (r *StateRecorder) SetRecordRead(val bool)
type WarriorData ¶
type WarriorData struct { Name string // Warrior Name Author string // Author Name Strategy string // Strategy including multiple lines Code []Instruction // Program Instructions Start int // Program Entry Point }
func CompileWarrior ¶ added in v0.1.9
func CompileWarrior(r io.Reader, config SimulatorConfig) (WarriorData, error)
func ParseLoadFile ¶
func ParseLoadFile(reader io.Reader, simConfig SimulatorConfig) (WarriorData, error)
func (*WarriorData) Copy ¶
func (w *WarriorData) Copy() *WarriorData
Copy creates a deep copy of a WarriorData object
type WarriorState ¶
type WarriorState uint8
const ( WarriorAdded WarriorState = iota WarriorAlive WarriorDead )