Sunday 24 January 2010

Cross Simulator Core Graphics

One difficulty when trying to write code to plot graphics in Redcode is the width of the core display. The four most popular simulators uses a variety of different widths:

Simulator
Display Width
pMARS SDL mode 0
157
pMARS SDL mode 2
126
CoreWin
128
ARES
80
nMars
various

The simplest solution is to define a variable for the width which can be modified by the person running the program. Alternatively it's possible to use the the predefined VERSION constant to determine the simulator:

Simulator
Version
pMARS SDL mode 0
92
pMARS SDL mode 2
92
CoreWin
230
ARES
0
nMars
93

Putting the two together we can write the following code which sets the correct width for pMARS SDL, CoreWin and ARES:

width equ ( (VERSION==92)*157 + (VERSION==230)*128 + (VERSION==0)*80 )

Thursday 7 January 2010

Fractals in Corewar

Koch Curve in RedcodeIn 1997 Anton Marsden organised a Corewar tournament with a difference.  The three rounds challenged players to use Redcode to solve a variety of problems.

In the third round of Anton's Corewar Tournament the challenge was to write a Redcode program to draw a pretty picture.  First place was taken by Ilmari Karonen with a fractal fern.

Here's my own attempt at creating a pretty picture in the core view of pMARS. It's a fractal known as the Koch Curve.

Here's the program, just 18 instructions:

width equ 157

        org    koch

        stack equ count+30

koch:   mov    #2-ptr, >stack
        jmp    count,  }move

        sub.a  #2,     move
        mov    #2-ptr, >stack
        jmp    count

        jmp    return, }move

count:  djn    koch,   #11
        mod.a  #8,     move
        div.a  #2,     move
        add.b  *move,  pos
        mul.a  #2,     move
pos:    add    #1,     koch+71*width/2+16
return: mov.ba <stack, ptr
ptr:    jmp    0,      >count

move:   dat    -1
        dat    -width
        dat    1
        dat    width

Have you tried to write a graphical display in Corewar?