The MARS C interface

Corewars is a venerable game and there are many implementations of MARS out there. pMARS, like I mentioned, is the defacto standard, implementing the '94 ICWS draft and adding later additions, such as new instructions and a thing called p-space, that we won't go into here. Corewars has never been serious business. Many of the MARS available online today have traded code back and forth over the years, have been written with varying levels of attention to correctness, and sometimes programmed for older machines where multiprocessing was a concern for research labs. Most make no distinction between their internal library interface and executable consumer, like Rust programs do. Some blessed few are designed to be embedded, or, at least, are extractions of older MARS codebases that can be extended. Some blessed few of those don't use static globals in their implementations and can be embedded in a multiprocessing environment, such as feruscore.

In the grand tradition of MARS implementations, we will take an existing, public domain MARS, whittle it down to a manageable core, and put a new spin on it. Specifically, the feruscore introduced in this chapter embeds exhaust 1.9.2 (http://corewar.co.uk/pihlaja/exhaust/), written by M. Joonas Pihlaja in the early 2000s. Pihlaja seems to have extracted select code from pMARS, especially in and around exhaust's main and parser functions. We aren't after any of that code. What we need is the simulator. This means that we can toss out anything to do with parsing, and any support code needed for exhaust's main function. The extracted code we require lives in the feruscore project root, in c_src/. The functions we'll embed are all implemented in c_src/sim.c. These are from c_src/sim.h:

void sim_free_bufs(mars_t* mars);
void sim_clear_core(mars_t* mars);

int sim_alloc_bufs(mars_t* mars);
int sim_load_warrior(mars_t* mars, uint32_t pos, 
const insn_t* const code, uint16_t len); int sim_mw(mars_t* mars, const uint16_t * const war_pos_tab,
uint32_t *death_tab );

The sim_alloc_bufs function is responsible for allocating the internal storage of a blank mars_t, the structure on which simulation is performed. sim_clear_core clears mars_t between rounds, setting core memory to DAT, and resetting any warrior queues. sim_load_warrior loads the warrior into core memory, the warrior really just being a pointer to an array of instructions—insn_t—with the len passed length. sim_mw runs the simulation, reading the warrior positions from war_pos_tab, and writing to death_tab when a warrior completely dies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.135.249