This is version 0.46, so don't expect much.
It is not complete and I expect lot of errors to show up.

Notes:
Application is printing reports to stdout.
Since it is not attached to console, you won't be able to see anything.
Only way to read reports is to
use redirection ( k16_corewars > reports.txt ).

I know that on screen instructions look bad,
I wanted them to be a bit ugly, but they turned out very ugly.

For keys to work correctly (in debugger) numlock must be on.

Command line syntax:

k16_corewars [-n] [options] [-w war1.war [-addr1] [war2.war [-addr2] [...]]]

-n : starts in noGUI mode
options:
-s num           :coresize
-l num           :max lenght
-p num           :processes
-c num           :cycles
-d num           :mindistance
-t num           :loadtime
-g               :pregs on (no effect in this version)
-r num           :rounds
-v num           :speed
-a               :pause on (off is default)
-e               :start  (emulator)
-b               :debug

defaults:
	coreleft=rules.core=4096;
	rules.size=256;
	rules.rounds=1;
	rules.cycle=1000000000;
	rules.dist=0;
	rules.ldtime=1;
	rules.pregs=0;
	rules.proc=400;
	wariorn=0;

	speed=4;
	pause=0;
	shell=SHELL_LOADER;
	scr.hex=0;
	scr.relative=0;	

The K16 virtual processor

Contains:
16 general-purpose registers named A0 to A15.
      Those registers are 16-bit.
Program counter PC,
      holds address of instruction that is currently executing.
Flag register GEL.
      It can be set to one of three states: greater (G), equal (E), less (L)

The basic data size is WORD (16-bit). All addresses are pointers to WORD.

Types of addressing
Register addressing	  	INC A6
Register indirect addressing    LD (A11), 5
Immediate addressing		JMP -7
Indirect addressing		LD (8), 5

To hide contents of PC, !!!all indirect addressing is also relative!!!.
This means that contents of PC is added to operand.

Instruction coding
Size of instruction not containing immediate data is one word.
If instruction has immediate data (immediate addressing or
indirect addressing), the instruction size is two words.
The second word contains immediate data.
In the first word, bits 15 to 8 contain instruction index (see file
ilist.dat), bits 7 to 4 destination register and bits 3 to 0 source register.
All instructions that are not listed in ilist.dat are considered invalid.


Assembler

Each line can contain label or instruction.
Example of line containing instruction:

LD A1, (data+1Ch-7)          ;Load  21st data word to register A1

It consist of instruction name, 0,1 or 2 operands
separated by comma, and comment which is ignored.

Examples for label:
Label1:
LD A5, Label1
LD A6, Label1
JMP Label1

The result is that A5 contains 0, A6 contains -2,
and last instruction is equivalent to JMP -4.


Instructions: (excerpts from code)

PARAMETERS: 
1.NAME
2.number of operands 0,1 or 2
3.is instruction saving result 1-yes 0-no
4.instruction handler

    init_InstrType("ADD",   2,1 ,&interpret.add  );
    init_InstrType("AND",   2,1 ,&interpret.and  );
    init_InstrType("CMP",   2,0 ,&interpret.sub  );
    init_InstrType("DEC",   1,1 ,&interpret.dec  );
    init_InstrType("INC",   1,1 ,&interpret.inc  );
    init_InstrType("DIV",   2,1 ,&interpret.div  );
    init_InstrType("MUL",   2,1 ,&interpret.mul  );
    init_InstrType("JMP",   1,0 ,&interpret.jmp  );
    init_InstrType("JG",    1,0 ,&interpret.jg  );
    init_InstrType("JL",    1,0 ,&interpret.jl  );
    init_InstrType("JE",    1,0 ,&interpret.je  );
    init_InstrType("JNG",   1,0 ,&interpret.jng  );
    init_InstrType("JNL",   1,0 ,&interpret.jnl  );
    init_InstrType("JNE",   1,0 ,&interpret.jne  );
    init_InstrType("LOOP",  2,1 ,&interpret.loop  );
    init_InstrType("LD",    2,1 ,&interpret.ld  );
    init_InstrType("NOP",   0,0 ,&interpret.nop  );
    init_InstrType("NEG",   1,1 ,&interpret.neg  );
    init_InstrType("NOT",   1,1 ,&interpret.not  );
    init_InstrType("OR",    2,1 ,&interpret.or  );
    init_InstrType("XOR",   2,1 ,&interpret.xor  );
    init_InstrType("SAR",   2,1 ,&interpret.sar  );
    init_InstrType("SHR",   2,1 ,&interpret.shr  );
    init_InstrType("SHL",   2,1 ,&interpret.shl  );
    init_InstrType("SUB",   2,1 ,&interpret.sub  );
    init_InstrType("SBC",   2,1 ,&interpret.sbc  );
    init_InstrType("ADC",   2,1 ,&interpret.adc  );
    init_InstrType("SPLIT", 1,0 ,&interpret.split  );
    init_InstrType("RND",   1,1 ,&interpret.rnd  );	


Instruction handlers:
    
    static void ld(void)        //ld
        {result=source2;}
    static void jmp(void)       //jmp
        {pcnext=procp->pc+(INT16)source1;}
    static void jg(void)        //jg
        {if (procp->flag==1) pcnext=procp->pc+(INT16)source1;}
    static void je(void)        //je
        {if (procp->flag==0) pcnext=procp->pc+(INT16)source1;}
    static void jl(void)        //jl
        {if (procp->flag==-1) pcnext=procp->pc+(INT16)source1;}
    static void jng(void)       //jng
        {if (procp->flag!=-1) pcnext=procp->pc+(INT16)source1;}
    static void jne(void)       //jne
        {if (procp->flag!=0) pcnext=procp->pc+(INT16)source1;}
    static void jnl(void)       //jnl
        {if (procp->flag!=3) pcnext=procp->pc+(INT16)source1;}
    static void loop(void)      //loop
        {result=source1-1;if (result!=0) pcnext=procp->pc+(INT16)source2;}
    static void split(void)             //split
    {        		    
        if (warp->processes.isfull()) return;
        warp->processes.insert( *(warp->processes.e()) );
		warp->processes.e()->cycle=1;
		warp->processes.e()->pc+=(INT16)source1;
		warp->processes.e()->pc&=maskcoresize;
		warp->processes.next();
    }
    static void add(void)
    {
        result=source1+source2;        
        if (result<source1) {procp->flag=1;return;} //overflow
        procp->flag=0;    
    }
    static void adc(void)      //adc
    {
        result=procp->flag;
        if (procp->flag>0) procp->flag=0;    
        result+=source1;
        if (result<source1) procp->flag++;
        result+=source2;
        if (result<source2) procp->flag++;    
    }
    static void sub(void)          //sub
    {
        result=source1-source2;
        if (source1>source2) {procp->flag=1;return;}
        if (source1<source2) {procp->flag=-1;return;}
        procp->flag=0;
    }
    static void sbc(void)          //sbc
    {
        if (procp->flag>0) procp->flag=0;
        result=procp->flag-source2;    
        if (source2>0) procp->flag=-1;
        result+=source1;
        if (result<source1) procp->flag++;        
        if (procp->flag==0 && result>0) procp->flag=1;
    }
    static void inc(void)       //inc
    {
        result=source1+1;
        if (result==0) {procp->flag=1;return;}
        procp->flag=0;
    }
    static void dec(void)    //dec
    {
        result=source1-1;
        if (result==-1) {procp->flag=-1;return;}
        if (result==0)  {procp->flag=0;return;}
        procp->flag=1;
    }
    static void nop(void)  //nop
        {}
    static void neg(void)  //neg
        {result=-source1;}
    static void not(void)  //not
        {result=~source1;}
    static void and(void)  //and
        {result=source1&source2;}
    static void or(void)  //or
        {result=source1|source2;}
    static void xor(void)  //xor
        {result=source1^source2;}
    static void mul(void)  //mul
        {result=source1*source2;}
    static void div(void)  //div
    {
        if (source2==0) {result=0xffff;procp->flag=-1;return;}
        result=source1/source2;
        if (result==0) procp->flag=0;
        else procp->flag=1;
    }
    static void shl(void)  //shl
        {result=source1<<source2;}
    static void sar(void)  //sar
        {result=short int(source1)>>source2;}
    static void shr(void)  //shr
        {result=source1>>source2;}    
    static WORD short_seed;  
    static void rnd(void) //rnd
    {
        short_seed++;
        short_seed*=9421;
        result=short_seed;
    }

*************************************
If you like this program or like to contribute to its future development,
send  $5 to address

Kresimir Cosic
Krusevica gumno 19
21000 Split
CROATIA
