[alt.sources] COREWARS for SUN

laba-2hd@e260-1e.berkeley.edu (An Existentialist) (01/25/89)

OK, i've been swamped by requests for SUN corewars. Again.  So, I'm giving
in and posting this.

This is the pixrect version of the corewars interpreter.  I'm posting only
the sections that are changed.  This is system specific.  It does not depend
on SUNVIEW, X, or any other windowing system.  You will need the pixrect
library to run this.  Do NOT overlap other windows on the window generated,
and to remove this window, just do a redraw.  (Yes, it does a write to the
frame buffer.  Pretty ugly, but.....

As usual, bug reports, etc, send to me, and any questions pertaining to
interface2.c direct to Case Larsen (laba-2ho@weaver.berkeley.edu).

Na Choon Piaw
laba-2hd@weaver.berkeley.edu

---------------------------------  cut here  --------------------------------
echo x - Makefile
cat >Makefile <<'!E!O!F!'
CFLAGS = -DSUN

interp: main.o load.o output.o play.o inst.o interface2.o 
	cc -o interp  main.o load.o output.o play.o inst.o interface2.o -lcurses -ltermcap -lpixrect

main.o:	interp.h main.c Makefile
	cc -c $(CFLAGS) main.c -o main.o

load.o: interp.h load.c
	cc -c $(CFLAG) load.c -o load.o

output.o: interp.h output.c Makefile
	cc -c $(CFLAGS) output.c -o output.o

play.o: play.c interp.h Makefile
	cc -c $(CFLAGS) play.c -o play.o

inst.o: inst.c interp.h
	cc -c $(FLAGS) inst.c -o inst.o
!E!O!F!
echo x - interface2.c
cat >interface2.c <<'!E!O!F!'
/**********************************************************************
 * 								      *
 * 								      *
 * 		   Copyright 1989 By Case T. Larsen		      *
 * 			 All Rights Reserved			      *
 * 				   				      *
 * 				   				      *
 **********************************************************************/

/*
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author makes no representations about the suitability of
this software for any purpose.  It is provided "as is"
without express or implied warranty.

*/


#include <pixrect/pixrect_hs.h>
#include "interp.h"

#define Y(x) (((x)/100)*5)
#define X(x) (((x)%100)*5)

static struct pixrect *screen;
static struct pixrect *bm;
static bitm[8000];
static xpos[8000], ypos[8000];

xcore_init ()
{
  int i;

  screen = pr_open("/dev/fb");
  bm = mem_create(500,400,1);
  for (i=0; i<8000;i++) {
    bitm[i] = 0;
    xpos[i] = X(i); ypos[i] = Y(i);
    zero(xpos[i],ypos[i]);
  }
  pr_rop (screen,0,0,500,400,PIX_SRC,bm,0,0);
} /* xcore_init */

zero (x,y)
     int x,y;
{
  pr_put (bm,x,y,1);
  pr_put (bm,x,y+1,1);
  pr_put (bm,x,y+2,1);
  pr_put (bm,x+1,y,1);
  pr_put (bm,x+1,y+1,1);
  pr_put (bm,x+1,y+2,1);
  pr_put (bm,x+2,y,1);
  pr_put (bm,x+2,y+1,1);
  pr_put (bm,x+2,y+2,1);
} /* zero */

one (x,y)
     int x,y;
{
  pr_put (bm,x,y,0);
  pr_put (bm,x,y+1,0);
  pr_put (bm,x,y+2,0);
  pr_put (bm,x+1,y,0);
  pr_put (bm,x+1,y+1,0);
  pr_put (bm,x+1,y+2,0);
  pr_put (bm,x+2,y,0);
  pr_put (bm,x+2,y+1,0);
  pr_put (bm,x+2,y+2,0);
} /* zero */

two (x,y)
     int x,y;
{
  pr_put (bm,x,y,1);
  pr_put (bm,x,y+1,0);
  pr_put (bm,x,y+2,0);
  pr_put (bm,x+1,y,0);
  pr_put (bm,x+1,y+1,1);
  pr_put (bm,x+1,y+2,0);
  pr_put (bm,x+2,y,0);
  pr_put (bm,x+2,y+1,0);
  pr_put (bm,x+2,y+2,1);
} /* zero */

xcore_display (mem)
     cell mem[];
{
  register int i;
  register int owner;

  for (i=0;i<8000;i++) {
    if (mem[i].lastmod != bitm[i]) {
      owner = bitm[i] = mem[i].lastmod;
      if (owner) {
	if (owner == 1) {
	  one (xpos[i],ypos[i]);
	} else
	  two (xpos[i],ypos[i]);
      } else
	zero (xpos[i],ypos[i]);
    }
  }
  pr_rop (screen,0,0,500,400,PIX_SRC,bm,0,0);
} /* xcore_display */

xcore_done ()
{
  pr_close(screen);
} /* xcore_done */
!E!O!F!
echo x - interp.h
cat >interp.h <<'!E!O!F!'
/* MARS redcode interpreter:
	Header file
	Started December '88
	Na Choon Piaw		*/

/*
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author makes no representations about the suitability of
this software for any purpose.  It is provided "as is"
without express or implied warranty.

*/


/* instruction set */
typedef enum { dat, mov, add, sub, jmp, jmz, jmn, djn, cmp, spl } instr;

/* addressing modes */
typedef enum { immed, direct, indirect } mode;

/* this is the new definition of a memory cell */
typedef struct
{
	instr	inst;		/* instruction */
	int	para1, para2;	/* first parameter, second parameter */
	mode	m1, m2;		/* addressing modes */
	int	lastmod;	/* last modified by */
} cell;

/* old assembler definition of memory cell */
typedef struct
{
	instr	inst;
	int	para1, para2;
	mode	m1, m2;
} memory;

/* doubly circularly linked list for streams of execution */
typedef struct tag0
{
	int		pc;	/* program counter */
	struct tag0	*next, *prev;
} stream;

/* interpreter limits */
#define MAXINST	200		/* maximum number of instructions */
#define SIZE	8000		/* size of array */
#define MAXPLAY	3		/* maximum number of players */
#define RANDIVISOR 33
!E!O!F!
echo x - play.c
cat >play.c <<'!E!O!F!'
/* play.c ---- main program for the interpreter.
   1.	play player 1
   2.	play player 2
   3.   output
   4.   check deaths
   5.   repeat

   --- 12/17/88   NCP                             */

/*
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author makes no representations about the suitability of
this software for any purpose.  It is provided "as is"
without express or implied warranty.

*/

#include <stdio.h>
#include "interp.h"

extern	stream *exe[];
extern  cell	a[];

int	dead(i)
int	i;
{
	if (exe[i])
		return 0;
	else
		return 1;
}


int	alive()
{
	int	i, j = 1;

	for (i = 1; i < MAXPLAY; i ++)
	{
		if (!exe[i])
		{
			j = 0;
			break;
		}
	}

	return(j);
}

int play(cycles)
long cycles;
{
	int	i;

	while (alive() && cycles)
	{
		for ( i = 1; i < MAXPLAY; i++)
		{
			execute(i);	/* play player n */
#ifdef DEBUG
			output();
#endif
#ifdef BIG
			output(cycles);
#endif
		}
#ifdef SUN
		if (!(cycles % 50))
			xcore_display(a);
#endif
#ifdef SMALL
		if (!(cycles % 100))
			output(cycles);
#endif
		cycles --;
	}

	if (dead(1) && !dead(2))
		return(2);		/* process 2 won */
	else if (dead(2) && !dead(1))
		return(1);		/* process 1 won */
	else
		return(0);		/* nobody won */
}

execute(i)
int	(i);
{
	instr	temp;			/* instruction */
	int	x;			/* cell to execute */
	int	pc;

	if (exe[i] == NULL)
		return;

	correct(&(exe[i] -> pc));
	x = pc = exe[i] -> pc;
	temp = a[x].inst;

	/* this really should have been a switch-case, but I'm using an
	   elongated if-else because the compiler doesn't accept it.   */

	if (temp == dat)
		Dat(i);
	else if (temp == mov)
		Mov(x, &pc, i);
	else if (temp == add)
		Add(x, &pc, i);
	else if (temp == sub)
		Sub(x, &pc, i);
	else if (temp == jmp)
		Jmp(x, &pc);
	else if (temp == jmz)
		Jmz(x, &pc);
	else if (temp == jmn)
		Jmn(x, &pc);
	else if (temp == djn)
		Djn(x, &pc, i);
	else if (temp == cmp)
		Cmp(x, &pc);
	else if (temp == spl)
		Spl(x, &pc, exe[i]);
	else
	{
		printf("Instruction not recognized\n");
		printf("--- execute\n");
		printf("opcode: %d", temp);
		printf(" executing no: %d", pc);
		printf(" process: %d\n", i);
		exit(1);
	}

	if (temp != dat)
		exe[i] -> pc = pc;

	if (exe[i] && (temp != dat))
		exe[i] = exe[i] -> next;
}
!E!O!F!
echo x - main.c
cat >main.c <<'!E!O!F!'
/* main program for MARS interpreter
   As usual, this does not do anything important.
   Just calls all the proper subroutines in the right order and
   opens files.
   -- 12/16/88         --- NCP        */

/*
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author makes no representations about the suitability of
this software for any purpose.  It is provided "as is"
without express or implied warranty.

*/

#include "interp.h"
#include <ctype.h>
#include <stdio.h>
#include <memory.h>
#include <curses.h>

long	atol();

/* memory array -- called "a" for easy typing */
cell	a[SIZE];

/* next execution for each player */
stream	*exe[MAXPLAY];

/* returns which player won */
int	play();

usage()
{
	printf("usage: interp cycles file1 file2\n");
	printf("--- main\n");
}

/* checks that every element in string is a digit */
int	number(str)
char	*str;
{
	int	i = 1;

	while (*str && i)
	{
		if (!isdigit(*str))
			i = 0;
		str++;
	}

	return i;
}

initialize()
{
	int	counter;	/* general-purpose counter */

	counter = (int) time(0);
#ifndef DEBUG
	initscr();		/* for "curses" library */
	scrollok(stdscr, 0);
	nl();
	clear();
	refresh();
#endif

#ifdef SUN
	xcore_init();
#endif
	srand(counter);
	for (counter = 0; counter++; counter < SIZE)
		memset(a[counter],0,sizeof(cell));
}

main(argc, argv)
int	argc;
char	*argv[];
{
	FILE	*f;
	int	errcode, result;

	initialize();		/* initialize all global variables */

	if (argc != 4)		/* too many or too few */
	{
		usage();
		exit(1);
	}

	if (!(number(argv[1])))
	{
		usage();
		exit(1);
	}

	if ((f = fopen(argv[2], "r")) == NULL)
	{
		printf("%s cannot be opened\n", argv[2]);
		exit(1);
	}

	errcode = load(f, 1);
	fclose(f);
	if (errcode == 1) {
		printf("main: Sorry, but %s is too large to load\n",argv[2]);
		exit(1);
	}


	if ((f = fopen(argv[3], "r")) == NULL)
	{
		printf("%s cannot be opened\n", argv[3]);
		exit(1);
	}

	errcode = load(f,2);
	fclose(f);
	if (errcode == 1) {
		printf("main: Sorry, but %s is too large to load\n",argv[3]);
		exit(1);
	}

#ifndef DEBUG
	clear();
#endif

	result = play(atol(argv[1]));

#ifndef DEBUG
/*	output(0); */
	move(21, 0);
	if (!result)
		printw("nobody won!");
	else
		printw("%s won!", argv[result + 1]);
	move(22, 0);
	printw("Hit any key to continue...");
	refresh();
	getch(errcode);
	endwin();
#endif
#ifdef SUN
	xcore_done();
#endif
}
!E!O!F!

An Existentialist
laba-2hd@weaver.berkeley.edu
%flames >/dev/null
"Do you think that if I stopped thinking so much I'd have less trouble?"