dcpu16

Virtual Machine and Assembler for Notch's DCPU-16 Architecture
git clone http://frotz.net/git/dcpu16.git
Log | Files | Refs | README

dcpu.c (2580B)


      1 /*
      2  * Copyright (c) 2012, Brian Swetland
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without 
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *   Redistributions of source code must retain the above copyright notice, 
      9  *   this list of conditions and the following disclaimer.
     10  *
     11  *   Redistributions in binary form must reproduce the above copyright 
     12  *   notice, this list of conditions and the following disclaimer in the 
     13  *   documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
     18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
     19  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
     21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <stdint.h>
     32 #include <string.h>
     33 #include <ctype.h>
     34 
     35 #include "emulator.h"
     36 
     37 extern u16 *disassemble(u16 *pc, char *out);
     38 
     39 void dumpheader(void) {
     40 	fprintf(stderr,
     41 		"PC   SP   OV   A    B    C    X    Y    Z    I    J    Instruction\n"
     42 		"---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----------\n");
     43 }
     44 
     45 void dumpstate(struct dcpu *d) {
     46 	char out[128];
     47 	disassemble(d->m + d->pc, out);
     48 	fprintf(stderr,
     49 		"%04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %s\n",
     50 		d->pc, d->sp, d->ov,
     51 		d->r[0], d->r[1], d->r[2], d->r[3],
     52 		d->r[4], d->r[5], d->r[6], d->r[7],
     53 		out);
     54 }
     55 
     56 void load(struct dcpu *d, const char *fn) {
     57 	FILE *fp;
     58 	char buf[128];
     59 	u16 n = 0;
     60 	if (!(fp = fopen(fn, "r"))) {
     61 		fprintf(stderr, "cannot open: %s\n", fn);
     62 		exit(1);
     63 	}	
     64 	while (!feof(fp) && fgets(buf, 128, fp)) {
     65 		if (!isalnum(buf[0]))
     66 			continue;
     67 		d->m[n++] = strtoul(buf, 0, 16);
     68 	}
     69 	fprintf(stderr,"< LOADED %d WORDS >\n", n);
     70 	fclose(fp);
     71 }
     72 	
     73 int main(int argc, char **argv) {
     74 	struct dcpu d;
     75 	memset(&d, 0, sizeof(d));
     76 
     77 	load(&d, argc > 1 ? argv[1] : "out.hex");
     78 
     79 	dumpheader();
     80 	for (;;) {
     81 		dumpstate(&d);
     82 		dcpu_step(&d);
     83 	}		
     84 	return 0;
     85 }
     86