diff options
| author | Jon duSaint | 2022-05-05 15:46:41 -0700 |
|---|---|---|
| committer | Jon duSaint | 2022-05-05 15:46:41 -0700 |
| commit | 6a290f10cf9aaa142db637834b54602286204394 (patch) | |
| tree | fed7333895a747244fc1518c3613281f6bf34c9a | |
| parent | 7c75c02f784be12343e2e4f82726f3a5f0ddb6a7 (diff) | |
xcpu: add old program
| -rw-r--r-- | README | 15 | ||||
| -rw-r--r-- | xcpu/Makefile | 27 | ||||
| -rw-r--r-- | xcpu/bootstrap.s | 169 | ||||
| -rw-r--r-- | xcpu/disassemble.c | 163 | ||||
| -rw-r--r-- | xcpu/errors.s | 16 | ||||
| -rw-r--r-- | xcpu/hash.c | 301 | ||||
| -rw-r--r-- | xcpu/hello.s | 65 | ||||
| -rw-r--r-- | xcpu/instruction_set.txt | 236 | ||||
| -rw-r--r-- | xcpu/lex.yy.c | 2267 | ||||
| -rw-r--r-- | xcpu/memory.c | 455 | ||||
| -rw-r--r-- | xcpu/test/compare.s | 12 | ||||
| -rw-r--r-- | xcpu/test/ltest.c | 51 | ||||
| -rw-r--r-- | xcpu/xas.c | 807 | ||||
| -rw-r--r-- | xcpu/xas.h | 106 | ||||
| -rw-r--r-- | xcpu/xas_lex.lex | 292 | ||||
| -rw-r--r-- | xcpu/xas_parse.y | 144 | ||||
| -rw-r--r-- | xcpu/xcpu.c | 1075 | ||||
| -rw-r--r-- | xcpu/xcpu_asm.h | 50 | ||||
| -rw-r--r-- | xcpu/xdis.c | 116 | ||||
| -rw-r--r-- | xcpu/xlib.s | 14 |
20 files changed, 6381 insertions, 0 deletions
@@ -156,6 +156,21 @@ UU and base64 encode/decode programs. My alarm clock for a few years. Launches a window with "snooze" and "off" buttons, and beeps annoyingly. +## XCPU + +Toy CPU emulator, used to investigate what an assembly language might +be when limited to four bits for the opcode. Read +`instruction_set.txt` to get an idea. I didn't finish the +disassembler program (though the disassembly routine works, it's just +not plugged int) or build the "standard library", but you can write +and run programs with this -- it turns out that four bits for an +opcode is possible, though makes for tedious to write assembly +language programs. Things I'd do differently now: remove `halt` and +`nop` instructions, replacing them with a reworked `cpuop` +instruction. Add in exception/interrupt vectors. Don't hardcode +memory regions, but rather allow different regions to have different +attributes. + ## xkill Kinda like `xkill`, but for windows. Results can be hilarious the first few times. diff --git a/xcpu/Makefile b/xcpu/Makefile new file mode 100644 index 0000000..f1cbccf --- /dev/null +++ b/xcpu/Makefile @@ -0,0 +1,27 @@ +# Makefile for XCPU + +CC = /usr/bin/gcc +CFLAGS = -Wall -g -O2 -lm +RM = /bin/rm +FLEX = /usr/bin/flex +BISON = /usr/bin/bison + +all : xas xcpu xdis bootstrap.xcpu + +xas : xas.c memory.c hash.c xas_parse.tab.c lex.yy.c + +xas_parse.tab.c: xas_parse.y + $(BISON) -d xas_parse.y + +lex.yy.c : xas_lex.lex + $(FLEX) xas_lex.lex + +xcpu: xcpu.c memory.c disassemble.c + +xdis: xdis.c disassemble.c + +bootstrap.xcpu: xas bootstrap.s + ./xas -a0 bootstrap.s + +clean : + $(RM) -f *~ xas xcpu lex.yy.c *.xcpu xas_parse.tab.* diff --git a/xcpu/bootstrap.s b/xcpu/bootstrap.s new file mode 100644 index 0000000..4ee8d09 --- /dev/null +++ b/xcpu/bootstrap.s @@ -0,0 +1,169 @@ + ;; bootstrap routines for xcpu + + _include xcpu_asm.h + + + ;; special I/O device + _define IO_DEV_EXE 0x7 + + ;; memory information + _define MEMORY_ADDR 0x110 + _define MEMORY_AMOUNT 0x10000 - 0x110 + _define STACK_ADDR 0x10000 - 0x1000 + _define IOPORTS_ADDR 0x0104 + + + ;; now start the bootstrap code + + ;; turn the debug flag on + ;cpuop CPU_DEBUG, 1 + + ;; configure memory to default + assign A, MEMORY_ADDR + assign B, MEMORY_AMOUNT + config CONFIG_MEMORY + ;; configure stack to default + assign A, STACK_ADDR + config CONFIG_STACK + ;; configure io ports to default + assign A, IOPORTS_ADDR + config CONFIG_IOPORTS + + ;; load program to run into memory + + ;; select device + assign A, IOPORT_DEVICE + push A + assign A, IO_DEV_EXE + cpuop CPU_MEMOP, 0 + memop A + + ;; select offset of program start offset + assign A, IOPORT_ADDR + push A + assign A, 0x4 + memop A + + ;; read program start offset + assign A, IOPORT_RW + push A + cpuop CPU_MEMOP, 1 + memop A + ;; A now contains program start offset + + ;; select program size offset + assign B, IOPORT_ADDR + push B + assign B, 0x8 + cpuop CPU_MEMOP, 0 + memop B + + ;; read program size + assign B, IOPORT_RW + push B + cpuop CPU_MEMOP, 1 + memop B + + ;; B now contains program size + + ;; add program start to program size + add B + + ;; select program load address offset + assign D, IOPORT_ADDR + push D + assign D, 0xc + cpuop CPU_MEMOP, 0 + memop D + + ;; program load address + assign D, IOPORT_RW + push D + cpuop CPU_MEMOP, 1 + memop D + + ;; now A has program start, B has program end, D has memory start + + ;; save load address on stack to call later + push D + + ;; keep program end on the stack + push B + + ;; keep start address in C + cpuop CPU_MOVE, 0 + move A + cpuop CPU_MOVE, 1 + move C + + ;; load program into memory + _label _loader_loop + + ;; point IO to current program offset (C) + assign A, IOPORT_ADDR + push A + cpuop CPU_MEMOP, 0 + memop C + + ;; read word into B + assign A, IOPORT_RW + push A + cpuop CPU_MEMOP, 1 + memop B + + ;; write word to current memory offset (D) + push D + cpuop CPU_MEMOP, 0 + memop B + + ;; increment C and D to point at next word + assign A, 4 + add C + add D + + ;; compare C with program end + pop A + push A + + compare C + + ;; continue loop if C < end + assign A, _loader_loop + push A + jump JUMP_LT + + + ;; reached end - clean up and hand off control + + ;; clear the stack + pop A + + ;; set up halt routine + assign A, halt_cpu + ;; save the load address first + pop B + push A + push B + + ;; zero out flags (except debug) + cpuop CPU_JUMP1, 0 + cpuop CPU_JUMP2, 0 + cpuop CPU_SLR, 0 + cpuop CPU_SRC, 0 + cpuop CPU_MOVE, 0 + + ;; zero out registers + assign A, 0 + assign B, 0 + assign C, 0 + assign D, 0 + + ;; start of memory is already on stack + + ;cpuop CPU_DEBUG, 1 + jump JUMP_UNCOND + + + ;; CPU termination routine + _label halt_cpu + halt diff --git a/xcpu/disassemble.c b/xcpu/disassemble.c new file mode 100644 index 0000000..b7181a1 --- /dev/null +++ b/xcpu/disassemble.c @@ -0,0 +1,163 @@ +/* XDIS + * Disassembly routines for XCPU. + */ + +#include "xas.h" + +uint32_t bswap4 (uint32_t in); + +struct instruction { + char *name; + char *(*args)(uint8_t *); +}; + +char *no_args (uint8_t *); +char *val_arg (uint8_t *); +char *reg_arg (uint8_t *); +char *assign_arg (uint8_t *); +char *cpuop_args (uint8_t *); + +struct instruction instruction_names[] = { + { "nop", no_args }, + { "jump", val_arg }, + { "push", reg_arg }, + { "pop", reg_arg }, + { "memop", reg_arg }, + { "cpuop", cpuop_args }, + { "assign", assign_arg }, + { "add", reg_arg }, + { "shift", reg_arg }, + { "and", reg_arg }, + { "or", reg_arg }, + { "not", reg_arg }, + { "move", reg_arg }, + { "compare", reg_arg }, + { "halt", no_args }, + { "config", val_arg } +}; + +char * +no_args (uint8_t *arg) +{ + char *ret = xmalloc (1); + ret[0] = '\0'; + return ret; +} + +char * +val_arg (uint8_t *arg) +{ + uint8_t val; + char *ret; + + val = arg[0] & 0xf; + + ret = xmalloc (8); + + snprintf (ret, 8, "0x%0hx", val); + + return ret; +} + +char * +reg_arg (uint8_t *arg) +{ + char *ret; + + ret = xmalloc (2); + ret[1] = '\0'; + + switch (arg[0] & 0xf) + { + case 1: + ret[0] = 'A'; + break; + case 2: + ret[0] = 'B'; + break; + case 4: + ret[0] = 'C'; + break; + case 8: + ret[0] = 'D'; + break; + default: + ret[0] = '?'; + break; + } + + return ret; +} + +char * +assign_arg (uint8_t *arg) +{ + char *ret; + uint32_t val; + + ret = xmalloc (32); + + switch (arg[0] & 0xf) + { + case 1: + ret[0] = 'A'; + break; + case 2: + ret[0] = 'B'; + break; + case 4: + ret[0] = 'C'; + break; + case 8: + ret[0] = 'D'; + break; + default: + ret[0] = '?'; + break; + } + + ret[1] = ','; + ret[2] = ' '; + + val = bswap4 (*(uint32_t *)(arg + 1)); + + snprintf (ret + 3, 29, "0x%08x", val); + + return ret; +} + +char * +cpuop_args (uint8_t *arg) +{ + char *ret; + + ret = xmalloc (5); + + snprintf (ret, 5, "%1d, %1d", arg[0] & 0x7, (arg[0] & 0x8) >> 3); + + return ret; +} + + +char * +disassemble_instruction (uint8_t *mem) +{ + uint8_t in; + char *dis, *op, *args; + int len; + + in = (mem[0] & 0xf0) >> 4; + + op = instruction_names[in].name; + args = instruction_names[in].args (mem); + + len = strlen (op) + strlen (args) + 2; + + dis = xmalloc (len); + + snprintf (dis, len, "%s %s", op, args); + + xfree (args); + + return dis; +} diff --git a/xcpu/errors.s b/xcpu/errors.s new file mode 100644 index 0000000..5446308 --- /dev/null +++ b/xcpu/errors.s @@ -0,0 +1,16 @@ + ;; errors.s - a file with several errors + + _include xcpu_asm.h + include foo.h ; invalid directive + _include foo.h ; doesn't exist + + cpuop CPU_DEBUG, 1 + assign A, B ; invalid second arg + add 1 ; invalid first arg + add A + assign B, bar + assign D, bar + + jump JUMP_FOO ; unresolved symbol + jump JUMP_UNCOND +
\ No newline at end of file diff --git a/xcpu/hash.c b/xcpu/hash.c new file mode 100644 index 0000000..dafb434 --- /dev/null +++ b/xcpu/hash.c @@ -0,0 +1,301 @@ +/* hash.c + * This file is part of Decomp - a decompiler. This file contains + * generic hash table manipulation routines. + * + * Copyright (C) 2001 Jonathan duSaint <dusaint@earthlink.net> + * + * Started around 20 October 2001. + */ + +#include <stdio.h> +#include <string.h> + +#include "xas.h" + + +#define DEFAULT_HASH_SIZE 8191 + + +/* hash_key + * Create an index from 0 to SIZE - 1 using the NUL terminated string KEY. + */ +unsigned long +hash_key (char *key, unsigned long size) +{ + int k; + unsigned long h, g; + + for (h = 0, k = 0; k < strlen (key); k++) + { + h = (h << 4) + (unsigned long)key[k]; + if ((g = h & 0xf0000000)) + { + h = h ^ (g >> 24); + h = h ^ g; + } + } + + return h % size; +} + + +/* hash_goodness + * Return the "goodness" value of the hash table which is the + * ratio of the number of filled buckets to the number of items + * total. The closer the value is to 1.0, the better. This, + * combined with the ratio of the number of filled buckets to + * the table size can be used to determine how well the hashing + * algorithm works. + */ +double +hash_goodness (hash_t table) +{ + double goodness; + unsigned long filled_buckets = 0, k; + + for (k = 0; k < table->size; k++) + if (table->table[k] != NULL) filled_buckets++; + + goodness = (double)filled_buckets / (double)table->items; + +/* if (debug) */ +/* printf ("hash goodness = %lf\nfilled:total = %lf\n", goodness, */ +/* (double)filled_buckets / (double)table->size); */ + + return (goodness); +} + + +/* hash_new + * Create a new hash table. If SIZE is 0, then the size is + * DEFAULT_HASH_SIZE, otherwise it is SIZE. + */ +hash_t +hash_new (unsigned long size) +{ + hash_t table; + + if (size == 0) size = DEFAULT_HASH_SIZE; + + table = xmalloc (sizeof (struct hash_table)); + + table->table = xmalloc (size * sizeof (struct hash_bucket)); + table->size = size; + table->items = 0; + + return (table); +} + +/* hash_add + * Add DATA to TABLE using KEY. + */ +int +hash_add (hash_t table, char *key, void *data) +{ + unsigned long index; + struct hash_bucket *tmp; + + index = hash_key (key, table->size); + + /* find empty bucket */ + if (table->table[index] != NULL) + { + tmp = table->table[index]; + while (tmp->next != NULL) tmp = tmp->next; + + tmp->next = xmalloc (sizeof (struct hash_bucket)); + tmp = tmp->next; + } + else + { + table->table[index] = xmalloc (sizeof (struct hash_bucket)); + tmp = table->table[index]; + } + + + /* insert new data */ + tmp->next = NULL; + tmp->key = key; + tmp->key = xmalloc (strlen (key) + 1); + strcpy (tmp->key, key); + tmp->data = data; + + table->items++; + + return (0); +} + +/* hash_get + * Return the data indexed by KEY or NULL if the slot is empty. + */ +void * +hash_get (hash_t table, char *key) +{ + unsigned long index; + struct hash_bucket *tmp; + + index = hash_key (key, table->size); + + tmp = table->table[index]; + + while (tmp != NULL) + { + if (!strcmp (key, tmp->key)) return tmp->data; + + tmp = tmp->next; + } + + /* only get here if the item wasn't found */ + return NULL; +} + +/* hash_remove + * Remove the data indexed by KEY from TABLE. + */ +void * +hash_remove (hash_t table, char *key) +{ + unsigned long index; + struct hash_bucket *tmp, *del = NULL; + void *data; + + index = hash_key (key, table->size); + + if (table->table[index] == NULL) return NULL; + + if (!strcmp (table->table[index]->key, key)) + { + del = table->table[index]; + table->table[index] = table->table[index]->next; + } + else + { + tmp = table->table[index]; + + while (tmp->next != NULL) + { + if (!strcmp (tmp->next->key, key)) break; + else tmp = tmp->next; + } + + if (tmp->next == NULL) return NULL; + + del = tmp->next; + tmp->next = del->next; + } + + data = del->data; + xfree (del->key); + xfree (del); + + return (data); +} + +/* hash_walk + * Walk through TABLE, returning keys one at a time. If TABLE is NULL, + * HASH_END is returned, and initialization is performed. + */ +char * +hash_walk (hash_t table) +{ + char *key; + static unsigned long index; + static struct hash_bucket *bucket; + + /* init */ + if (table == NULL) + { + index = 0; + bucket = NULL; + return HASH_END; + } + + /* looking for buckets */ + if (bucket == NULL) + { + while (table->table[index] == NULL) + { + index++; + if (index >= table->size) return HASH_END; /* signal done */ + } + + bucket = table->table[index]; + } + + key = bucket->key; + bucket = bucket->next; + + return (key); +} + +/* hash_delete + * Delete TABLE. + */ +void +hash_delete (hash_t table) +{ + char *key; + + hash_walk (NULL); + + while ((key = hash_walk (table)) != HASH_END) + hash_remove (table, key); + + xfree (table->table); + xfree (table); +} + +/* hash_rehash + * Resize TABLE. If SIZE is 0, then the new table is twice the size + * of the old table, otherwise it has SIZE buckets. + */ +hash_t +hash_rehash (hash_t table, unsigned long size) +{ + char *key; + hash_t new; + + if (size == 0) + new = hash_new (2 * table->size); + else + new = hash_new (size); + + hash_walk (NULL); + + while ((key = hash_walk (table)) != HASH_END) + { + /*printf (" adding %#lx\n", key);*/ + hash_add (new, key, hash_get (table, key)); + hash_remove (table, key); + } + + hash_delete (table); + + return (new); +} + +/* hash_print + * Print all of the items in the hash table in a human-readable format. + */ +void +hash_print (hash_t table) +{ + unsigned long k; + struct hash_bucket *tmp; + + for (k = 0; k < table->size; k++) + { + if (table->table[k] == NULL) continue; + + printf ("%ld: %s -> %p\n", k, table->table[k]->key, + table->table[k]->data); + + tmp = table->table[k]->next; + + while (tmp != NULL) + { + printf (" --> %s -> %p\n", tmp->key, tmp->data); + tmp = tmp->next; + } + } +} diff --git a/xcpu/hello.s b/xcpu/hello.s new file mode 100644 index 0000000..236ab9f --- /dev/null +++ b/xcpu/hello.s @@ -0,0 +1,65 @@ + ;; hello.s - hello world for XCPU + + _include xcpu_asm.h + + _label hello + + ;cpuop CPU_DEBUG, 1 + ;; select console device + assign A, IOPORT_DEVICE + push A + assign A, IO_DEV_CONSOLE + cpuop CPU_MEMOP, CPU_MEMOP_WRITE + memop A + + ;; prepare console write address + assign B, IOPORT_RW + + ;; get string start and end address + assign A, end_of_string + push A + assign C, start_of_string + + ;; preset shifting + cpuop CPU_SLR, CPU_SHIFT_RIGHT + cpuop CPU_SRC, CPU_SHIFT_REG + + _label loop + + ;; get next character + cpuop CPU_MEMOP, CPU_MEMOP_READ + push C + memop D + + ;; shift it to be in byte 0 + assign A, 24 + shift D + + ;; write it out + push B + cpuop CPU_MEMOP, CPU_MEMOP_WRITE + memop D + + ;; find out if we're at the end yet + assign A, 1 + add C + pop A + push A + compare C + + assign A, loop + push A + jump JUMP_LT + + ;; clean up + pop A + + ;; terminate xcpu + + jump JUMP_UNCOND + + ;; data section + + _label start_of_string + _immed "Hello there!\n" + _label end_of_string diff --git a/xcpu/instruction_set.txt b/xcpu/instruction_set.txt new file mode 100644 index 0000000..d28b253 --- /dev/null +++ b/xcpu/instruction_set.txt @@ -0,0 +1,236 @@ +Instruction set and CPU description for XCPU. + +** CPU Description ** + +XCPU is an SRISC processor (Super-Reduced Instruction Set +Computer). The CPU has four general purpose 32 bit registers, referred +to as A, B, C, and D. In addition , 8 CPU flags are present in the +status register to control various aspects of operation. XCPU also +contains a 32 bit instruction pointer (IP), a 32 bit stack pointer +(SP), and a 32 bit move register (M). IP is initialized by XCPU +itself, but SP must be initialized using the config instruction. All +instructions are exactly one byte, except ASSIGN, which is one byte +plus four bytes of immediate data. + + +** CPU Status Bits ** + + 0 - Jump control + 0 - compare returned equal + 1 - compare returned not equal + 1 - Jump control + 0 - compare returned less than + 1 - compare returned greater than + 2 - Memory operation control + 0 - store + 1 - load + 3 - Shift control + 0 - left shift + 1 - right shift + 4 - Shift control + 0 - regular shift + 1 - circular shift + 5 - Move control + 0 - move to M + 1 - move from M + 6 - Unassigned + 0 - + 1 - + 7 - Debug bit + 0 - debug mode off + 1 - debug mode on + + +** Instruction Set ** + +* 0 - nop + Do nothing at all. + + +* 1 - jump + Jump or conditional jump to address on stack. Jump type controlled + by status register. To returning from a subroutine, use an + unconditional jump without placing an argument on the stack. + + Args: + 0 - unconditional jump + 1 - jump if equal (bit 0) + 2 - jump if not equal (bit 0) + 3 - jump if less than (bit 1) + 4 - jump if greater than (bit 1) + 5 - + 6 - + 7 - call subroutine + + +* 2 - push + Push value in register onto stack. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* 3 - pop + Pop value on stack into register. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* 4 - memop + Perform memory operation to/from register, getting address from + stack. Load if status bit 2 set. Store if status bit 2 clear. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* 5 - cpuop + Perform operation on CPU status bit. + + Args: + bit 3 + 0 - clear + 1 - set + bits 2:0 + Corresponding CPU bit (0-7) + + +* 6 - assign + Assign following 32 bits to a register. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* 7 - add + Add register to A, storing the result in register. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* 8 - shift + Shift register by amount in A. Type of shift controlled by CPU + status bits 3 and 4. + + Status bits: + 3 clear - left + 3 set - right + 4 clear - regular + 4 set - circular + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* 9 - and + AND register with A, storing the result in register. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* a - or + OR register with A, storing the result in register. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* b - not + Invert bits in register. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* c - move + Move data to or from M, controlled by CPU flag 5. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* d - compare + Compare a register with A, setting status bits 0 and 1 accordingly - + i.e. reg .operator. A. + + Args: + 1 - A register + 2 - B register + 4 - C register + 8 - D register + + +* e - halt + Shut down the CPU. + +* f - config + Configure external devices. + + Args: + 0 - configure memory at address in A with amount in B + 1 - configure stack at address in A + 2 - configure io ports at address in A + + +** XCPU file format ** + +bytes 0-3: XCPU + +bytes 4-7: offset from start of file to start of object code + +bytes 8-b: size of object code + +bytes c-f: load address (or 0) + +bytes 10-?: (possibly) debug information + +rest of file: object code + + +** XCPU Initialization ** + +XCPU begins executing at address 0, which is mapped on startup to +ROM. ROM equivalent is contained in bootstrap.xcpu (source in +bootstrap.s). The startup routine initializes memory to start at 0x110 +(the first 256 bytes remain mapped to ROM) and be of size 0xfef0 (64k +minus ROM minus IO), initializes the stack to start at 4k before the +end of memory, and initializes the IO port to start at 0x104. That +leaves from 0x110 to 0xf000 available for the program. At this point, +the program is loaded from device 0 to address 0x110. Once the program +is loaded, the address of the start of memory is placed in A and +control jumps to 0x110. A pointer to the CPU termination routine is +located at 0x100 and calling this routine is how the program should +exit. diff --git a/xcpu/lex.yy.c b/xcpu/lex.yy.c new file mode 100644 index 0000000..64222a9 --- /dev/null +++ b/xcpu/lex.yy.c @@ -0,0 +1,2267 @@ + +#line 3 "lex.yy.c" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 5 +#define YY_FLEX_SUBMINOR_VERSION 35 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */ + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include <inttypes.h> +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; +#endif /* ! C99 */ + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! FLEXINT_H */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +/* C99 requires __STDC__ to be defined as 1. */ +#if defined (__STDC__) + +#define YY_USE_CONST + +#endif /* defined (__STDC__) */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart(yyin ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#define YY_BUF_SIZE 16384 +#endif + +/* The state buf must be large enough to hold one state per character in the main buffer. + */ +#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +extern int yyleng; + +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + + /* Note: We specifically omit the test for yy_rule_can_match_eol because it requires + * access to the local variable yy_act. Since yyless() is a macro, it would break + * existing scanners that call yyless() from OUTSIDE yylex. + * One obvious solution it to make yy_act a global. I tried that, and saw + * a 5% performance hit in a non-yylineno scanner, because yy_act is + * normally declared as a register variable-- so it is not worth it. + */ + #define YY_LESS_LINENO(n) \ + do { \ + int yyl;\ + for ( yyl = n; yyl < yyleng; ++yyl )\ + if ( yytext[yyl] == '\n' )\ + --yylineno;\ + }while(0) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET \ + (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, (yytext_ptr) ) + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* Stack of input buffers. */ +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ +static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ + ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ + : NULL) + +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; +static int yy_n_chars; /* number of characters read into yy_ch_buf */ +int yyleng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = (char *) 0; +static int yy_init = 0; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void yyrestart (FILE *input_file ); +void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); +void yy_delete_buffer (YY_BUFFER_STATE b ); +void yy_flush_buffer (YY_BUFFER_STATE b ); +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); +void yypop_buffer_state (void ); + +static void yyensure_buffer_stack (void ); +static void yy_load_buffer_state (void ); +static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ); + +#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER ) + +YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); +YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); +YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len ); + +void *yyalloc (yy_size_t ); +void *yyrealloc (void *,yy_size_t ); +void yyfree (void * ); + +#define yy_new_buffer yy_create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! YY_CURRENT_BUFFER ){ \ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! YY_CURRENT_BUFFER ){\ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +#define yywrap(n) 1 +#define YY_SKIP_YYWRAP + +typedef unsigned char YY_CHAR; + +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; + +typedef int yy_state_type; + +extern int yylineno; + +int yylineno = 1; + +extern char *yytext; +#define yytext_ptr yytext + +static yy_state_type yy_get_previous_state (void ); +static yy_state_type yy_try_NUL_trans (yy_state_type current_state ); +static int yy_get_next_buffer (void ); +static void yy_fatal_error (yyconst char msg[] ); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + yyleng = (size_t) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; + +#define YY_NUM_RULES 35 +#define YY_END_OF_BUFFER 36 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info + { + flex_int32_t yy_verify; + flex_int32_t yy_nxt; + }; +static yyconst flex_int16_t yy_accept[113] = + { 0, + 0, 0, 36, 34, 33, 32, 34, 31, 34, 1, + 1, 2, 8, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 29, 0, 5, 0, 0, 0, 2, + 0, 0, 29, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 29, 29, 29, 19, 29, 29, 29, + 4, 0, 0, 30, 3, 29, 29, 29, 29, 16, + 18, 29, 29, 29, 29, 29, 29, 29, 29, 9, + 20, 12, 29, 29, 7, 6, 29, 29, 29, 29, + 29, 29, 29, 29, 23, 10, 29, 21, 11, 29, + 29, 29, 29, 29, 29, 29, 29, 14, 13, 17, + + 29, 28, 29, 27, 15, 29, 24, 26, 29, 22, + 25, 0 + } ; + +static yyconst flex_int32_t yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 4, 1, 5, 6, 1, 1, 1, 7, 1, + 1, 1, 1, 1, 1, 8, 1, 9, 10, 11, + 11, 11, 11, 11, 11, 11, 11, 1, 12, 1, + 1, 1, 1, 1, 13, 13, 13, 13, 14, 14, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 1, 16, 1, 1, 17, 1, 18, 19, 20, 21, + + 22, 23, 24, 25, 26, 27, 15, 28, 29, 30, + 31, 32, 15, 33, 34, 35, 36, 37, 15, 38, + 15, 15, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static yyconst flex_int32_t yy_meta[39] = + { 0, + 1, 1, 2, 1, 1, 1, 1, 3, 4, 4, + 4, 1, 4, 4, 4, 1, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4 + } ; + +static yyconst flex_int16_t yy_base[118] = + { 0, + 0, 0, 245, 246, 246, 246, 239, 246, 227, 31, + 35, 39, 234, 233, 44, 43, 47, 45, 46, 49, + 50, 51, 52, 60, 235, 234, 231, 59, 0, 82, + 229, 86, 228, 54, 90, 93, 53, 94, 78, 95, + 102, 59, 105, 106, 108, 109, 227, 110, 113, 114, + 246, 227, 226, 0, 0, 120, 119, 129, 131, 224, + 223, 125, 121, 123, 124, 122, 128, 144, 146, 222, + 221, 220, 148, 151, 246, 246, 150, 155, 153, 156, + 158, 154, 157, 159, 219, 218, 161, 217, 216, 162, + 171, 163, 172, 177, 179, 178, 180, 209, 205, 194, + + 181, 192, 186, 191, 190, 184, 187, 182, 188, 118, + 115, 246, 211, 215, 217, 219, 52 + } ; + +static yyconst flex_int16_t yy_def[118] = + { 0, + 112, 1, 112, 112, 112, 112, 113, 112, 114, 115, + 115, 115, 116, 116, 116, 116, 116, 116, 116, 116, + 116, 116, 116, 116, 113, 113, 112, 112, 117, 115, + 115, 115, 116, 116, 116, 116, 116, 116, 116, 116, + 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, + 112, 112, 112, 117, 32, 116, 116, 116, 116, 116, + 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, + 116, 116, 116, 116, 112, 112, 116, 116, 116, 116, + 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, + 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, + + 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, + 116, 0, 112, 112, 112, 112, 112 + } ; + +static yyconst flex_int16_t yy_nxt[285] = + { 0, + 4, 5, 6, 5, 7, 8, 9, 4, 10, 11, + 12, 8, 13, 14, 14, 4, 15, 16, 14, 17, + 14, 14, 14, 14, 18, 14, 19, 14, 20, 21, + 22, 23, 14, 24, 14, 14, 14, 14, 29, 30, + 30, 30, 29, 30, 30, 30, 29, 30, 30, 30, + 29, 29, 29, 29, 29, 54, 29, 29, 29, 29, + 29, 29, 42, 37, 34, 51, 29, 29, 32, 35, + 44, 36, 38, 60, 52, 56, 39, 40, 41, 45, + 46, 43, 48, 47, 50, 29, 66, 49, 53, 29, + 30, 30, 30, 29, 55, 55, 55, 29, 55, 55, + + 29, 29, 29, 55, 55, 55, 55, 55, 55, 29, + 59, 62, 29, 29, 61, 29, 29, 29, 57, 58, + 29, 29, 29, 63, 64, 29, 29, 29, 29, 29, + 29, 29, 29, 67, 68, 29, 29, 65, 29, 74, + 70, 72, 77, 71, 69, 83, 73, 78, 79, 80, + 81, 29, 82, 29, 84, 29, 85, 29, 29, 86, + 29, 29, 29, 29, 29, 29, 29, 88, 29, 29, + 29, 96, 89, 90, 87, 91, 92, 94, 29, 29, + 93, 95, 97, 102, 29, 29, 29, 29, 29, 29, + 98, 29, 99, 29, 29, 29, 100, 29, 29, 29, + + 101, 29, 108, 107, 104, 110, 109, 103, 105, 111, + 106, 25, 29, 25, 25, 27, 29, 27, 27, 31, + 31, 33, 33, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 76, 75, 29, 29, 29, 51, 26, 26, + 29, 29, 28, 26, 112, 3, 112, 112, 112, 112, + 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, + 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, + 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, + 112, 112, 112, 112 + } ; + +static yyconst flex_int16_t yy_chk[285] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, + 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, + 16, 15, 18, 19, 17, 117, 20, 21, 22, 23, + 37, 34, 18, 16, 15, 28, 42, 24, 10, 15, + 20, 15, 16, 37, 28, 34, 16, 17, 17, 20, + 21, 19, 23, 22, 24, 39, 42, 23, 28, 30, + 30, 30, 30, 32, 32, 32, 32, 35, 32, 32, + + 36, 38, 40, 32, 32, 32, 32, 32, 32, 41, + 36, 39, 43, 44, 38, 45, 46, 48, 35, 35, + 49, 50, 111, 40, 40, 110, 57, 56, 63, 66, + 64, 65, 62, 43, 44, 67, 58, 41, 59, 50, + 46, 48, 56, 46, 45, 64, 49, 57, 58, 59, + 62, 68, 63, 69, 65, 73, 66, 77, 74, 67, + 79, 82, 78, 80, 83, 81, 84, 69, 87, 90, + 92, 82, 73, 74, 68, 77, 78, 80, 91, 93, + 79, 81, 83, 92, 94, 96, 95, 97, 101, 108, + 84, 106, 87, 103, 107, 109, 90, 105, 104, 102, + + 91, 100, 101, 97, 94, 106, 103, 93, 95, 109, + 96, 113, 99, 113, 113, 114, 98, 114, 114, 115, + 115, 116, 116, 89, 88, 86, 85, 72, 71, 70, + 61, 60, 53, 52, 47, 33, 31, 27, 26, 25, + 14, 13, 9, 7, 3, 112, 112, 112, 112, 112, + 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, + 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, + 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, + 112, 112, 112, 112 + } ; + +/* Table of booleans, true if rule could match eol. */ +static yyconst flex_int32_t yy_rule_can_match_eol[36] = + { 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, }; + +static yy_state_type yy_last_accepting_state; +static char *yy_last_accepting_cpos; + +extern int yy_flex_debug; +int yy_flex_debug = 0; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *yytext; +#line 1 "xas_lex.lex" +/* xas_lex.lex + * Lexical analyzer definition file for XAS, an assembler for XCPU. + * + */ +#line 7 "xas_lex.lex" +#include "xas.h" +#line 574 "lex.yy.c" + +#define INITIAL 0 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include <unistd.h> +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +static int yy_init_globals (void ); + +/* Accessor methods to globals. + These are made visible to non-reentrant scanners for convenience. */ + +int yylex_destroy (void ); + +int yyget_debug (void ); + +void yyset_debug (int debug_flag ); + +YY_EXTRA_TYPE yyget_extra (void ); + +void yyset_extra (YY_EXTRA_TYPE user_defined ); + +FILE *yyget_in (void ); + +void yyset_in (FILE * in_str ); + +FILE *yyget_out (void ); + +void yyset_out (FILE * out_str ); + +int yyget_leng (void ); + +char *yyget_text (void ); + +int yyget_lineno (void ); + +void yyset_lineno (int line_number ); + +YYSTYPE * yyget_lval (void ); + +void yyset_lval (YYSTYPE * yylval_param ); + + YYLTYPE *yyget_lloc (void ); + + void yyset_lloc (YYLTYPE * yylloc_param ); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap (void ); +#else +extern int yywrap (void ); +#endif +#endif + + static void yyunput (int c,char *buf_ptr ); + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#ifdef __cplusplus +static int yyinput (void ); +#else +static int input (void ); +#endif + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#define YY_READ_BUF_SIZE 8192 +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO fwrite( yytext, yyleng, 1, yyout ) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ + { \ + int c = '*'; \ + int n; \ + for ( n = 0; n < max_size && \ + (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else \ + { \ + errno=0; \ + while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ + { \ + if( errno != EINTR) \ + { \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + break; \ + } \ + errno=0; \ + clearerr(yyin); \ + } \ + }\ +\ + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int yylex \ + (YYSTYPE * yylval_param,YYLTYPE * yylloc_param ); + +#define YY_DECL int yylex \ + (YYSTYPE * yylval_param, YYLTYPE * yylloc_param ) +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + register yy_state_type yy_current_state; + register char *yy_cp, *yy_bp; + register int yy_act; + + YYSTYPE * yylval; + + YYLTYPE * yylloc; + +#line 22 "xas_lex.lex" + + +#line 773 "lex.yy.c" + + yylval = yylval_param; + + yylloc = yylloc_param; + + if ( !(yy_init) ) + { + (yy_init) = 1; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! (yy_start) ) + (yy_start) = 1; /* first start state */ + + if ( ! yyin ) + yyin = stdin; + + if ( ! yyout ) + yyout = stdout; + + if ( ! YY_CURRENT_BUFFER ) { + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_load_buffer_state( ); + } + + while ( 1 ) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of yytext. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); +yy_match: + do + { + register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 113 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + ++yy_cp; + } + while ( yy_base[yy_current_state] != 246 ); + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + if ( yy_act == 0 ) + { /* have to back up */ + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + + if ( yy_act != YY_END_OF_BUFFER && yy_rule_can_match_eol[yy_act] ) + { + int yyl; + for ( yyl = 0; yyl < yyleng; ++yyl ) + if ( yytext[yyl] == '\n' ) + + yylineno++; +; + } + +do_action: /* This label is used only to access EOF actions. */ + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + +case 1: +YY_RULE_SETUP +#line 24 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + yylval->val = strtoul (yytext, NULL, 2); + + return BIT; +} + YY_BREAK +case 2: +YY_RULE_SETUP +#line 34 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + yylval->val = strtoul (yytext, NULL, 0); + + return NUMBER; +} + YY_BREAK +case 3: +YY_RULE_SETUP +#line 44 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + yylval->val = strtoul (yytext, NULL, 16); + + return NUMBER; +} + YY_BREAK +case 4: +YY_RULE_SETUP +#line 54 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yylval->character = yytext[1]; + + return CHARACTER; +} + YY_BREAK +case 5: +YY_RULE_SETUP +#line 63 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yytext[yyleng-1] = 0; + + yylval->id = xmalloc (yyleng - 1); + strncpy (yylval->string, yytext + 1, yyleng - 2); + + return STRING; +} + YY_BREAK +case 6: +YY_RULE_SETUP +#line 75 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yylval->character = '\n'; + + return CHARACTER; +} + YY_BREAK +case 7: +YY_RULE_SETUP +#line 84 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yylval->character = '\\'; + + return CHARACTER; +} + YY_BREAK +case 8: +YY_RULE_SETUP +#line 93 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yylval->reg = yytext[0]; + + return REGISTER; +} + YY_BREAK +case 9: +YY_RULE_SETUP +#line 102 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return NOP; +} + YY_BREAK +case 10: +YY_RULE_SETUP +#line 109 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return JUMP; +} + YY_BREAK +case 11: +YY_RULE_SETUP +#line 116 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return PUSH; +} + YY_BREAK +case 12: +YY_RULE_SETUP +#line 123 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return POP; +} + YY_BREAK +case 13: +YY_RULE_SETUP +#line 130 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return MEMOP; +} + YY_BREAK +case 14: +YY_RULE_SETUP +#line 137 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return CPUOP; +} + YY_BREAK +case 15: +YY_RULE_SETUP +#line 144 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return ASSIGN; +} + YY_BREAK +case 16: +YY_RULE_SETUP +#line 151 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return ADD; +} + YY_BREAK +case 17: +YY_RULE_SETUP +#line 158 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return SHIFT; +} + YY_BREAK +case 18: +YY_RULE_SETUP +#line 165 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return AND; +} + YY_BREAK +case 19: +YY_RULE_SETUP +#line 172 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return OR; +} + YY_BREAK +case 20: +YY_RULE_SETUP +#line 179 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return NOT; +} + YY_BREAK +case 21: +YY_RULE_SETUP +#line 186 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return MOVE; +} + YY_BREAK +case 22: +YY_RULE_SETUP +#line 193 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return COMPARE; +} + YY_BREAK +case 23: +YY_RULE_SETUP +#line 200 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return HALT; +} + YY_BREAK +case 24: +YY_RULE_SETUP +#line 207 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return CONFIG; +} + YY_BREAK +case 25: +YY_RULE_SETUP +#line 214 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return INCLUDE; +} + YY_BREAK +case 26: +YY_RULE_SETUP +#line 221 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return DEFINE; +} + YY_BREAK +case 27: +YY_RULE_SETUP +#line 228 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return LABEL; +} + YY_BREAK +case 28: +YY_RULE_SETUP +#line 235 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + return IMMED; +} + YY_BREAK +case 29: +YY_RULE_SETUP +#line 242 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + +/* if (line_pos == 1) */ +/* return MACROREF; */ + + yylval->id = xmalloc (yyleng + 1); + strncpy (yylval->id, yytext, yyleng); + + return ID; +} + YY_BREAK +case 30: +YY_RULE_SETUP +#line 257 "xas_lex.lex" +{ + line_pos++; + line_col += yyleng; + + yylval->id = xmalloc (yyleng + 1); + strncpy (yylval->id, yytext, yyleng); + + return FILENAME; +} + YY_BREAK +case 31: +YY_RULE_SETUP +#line 267 "xas_lex.lex" +{ + int c; + while ((c = input ()) != EOF) + { + if (c == '\n') + { + unput (c); + break; + } + } +} + YY_BREAK +case 32: +/* rule 32 can match eol */ +YY_RULE_SETUP +#line 279 "xas_lex.lex" +{ + line_no++; + line_pos = 0; + line_col = 0; +} + YY_BREAK +case 33: +YY_RULE_SETUP +#line 285 "xas_lex.lex" +{ + line_col++; +} + YY_BREAK +case 34: +YY_RULE_SETUP +#line 289 "xas_lex.lex" +{ + line_col++; + return yytext[0]; +} + YY_BREAK +case 35: +YY_RULE_SETUP +#line 293 "xas_lex.lex" +YY_FATAL_ERROR( "flex scanner jammed" ); + YY_BREAK +#line 1248 "lex.yy.c" +case YY_STATE_EOF(INITIAL): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if ( yywrap( ) ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = + (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ +} /* end of yylex */ + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +static int yy_get_next_buffer (void) +{ + register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + register char *source = (yytext_ptr); + register int number_to_move, i; + int ret_val; + + if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + int num_to_read = + YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER; + + int yy_c_buf_p_offset = + (int) ((yy_c_buf_p) - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + int new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - + number_to_move - 1; + + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), + (yy_n_chars), (size_t) num_to_read ); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if ( (yy_n_chars) == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart(yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + if ((yy_size_t) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { + /* Extend the array by 50%, plus the number we really need. */ + yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ); + if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); + } + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + + static yy_state_type yy_get_previous_state (void) +{ + register yy_state_type yy_current_state; + register char *yy_cp; + + yy_current_state = (yy_start); + + for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) + { + register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 113 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) +{ + register int yy_is_jam; + register char *yy_cp = (yy_c_buf_p); + + register YY_CHAR yy_c = 1; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 113 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + yy_is_jam = (yy_current_state == 112); + + return yy_is_jam ? 0 : yy_current_state; +} + + static void yyunput (int c, register char * yy_bp ) +{ + register char *yy_cp; + + yy_cp = (yy_c_buf_p); + + /* undo effects of setting up yytext */ + *yy_cp = (yy_hold_char); + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + register int number_to_move = (yy_n_chars) + 2; + register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; + register char *source = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; + + while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + if ( c == '\n' ){ + --yylineno; + } + + (yytext_ptr) = yy_bp; + (yy_hold_char) = *yy_cp; + (yy_c_buf_p) = yy_cp; +} + +#ifndef YY_NO_INPUT +#ifdef __cplusplus + static int yyinput (void) +#else + static int input (void) +#endif + +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + int offset = (yy_c_buf_p) - (yytext_ptr); + ++(yy_c_buf_p); + + switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart(yyin ); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap( ) ) + return EOF; + + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + offset; + break; + } + } + } + + c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve yytext */ + (yy_hold_char) = *++(yy_c_buf_p); + + if ( c == '\n' ) + + yylineno++; +; + + return c; +} +#endif /* ifndef YY_NO_INPUT */ + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ + void yyrestart (FILE * input_file ) +{ + + if ( ! YY_CURRENT_BUFFER ){ + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_init_buffer(YY_CURRENT_BUFFER,input_file ); + yy_load_buffer_state( ); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ + void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) +{ + + /* TODO. We should be able to replace this entire function body + * with + * yypop_buffer_state(); + * yypush_buffer_state(new_buffer); + */ + yyensure_buffer_stack (); + if ( YY_CURRENT_BUFFER == new_buffer ) + return; + + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + yy_load_buffer_state( ); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + +static void yy_load_buffer_state (void) +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ + YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer(b,file ); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with yy_create_buffer() + * + */ + void yy_delete_buffer (YY_BUFFER_STATE b ) +{ + + if ( ! b ) + return; + + if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yyfree((void *) b->yy_ch_buf ); + + yyfree((void *) b ); +} + +#ifndef _UNISTD_H /* assume unistd.h has isatty() for us */ +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __THROW /* this is a gnuism */ +extern int isatty (int ) __THROW; +#else +extern int isatty (int ); +#endif +#ifdef __cplusplus +} +#endif +#endif + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a yyrestart() or at EOF. + */ + static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) + +{ + int oerrno = errno; + + yy_flush_buffer(b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then yy_init_buffer was _probably_ + * called from yyrestart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if (b != YY_CURRENT_BUFFER){ + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; + + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ + void yy_flush_buffer (YY_BUFFER_STATE b ) +{ + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == YY_CURRENT_BUFFER ) + yy_load_buffer_state( ); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) +{ + if (new_buffer == NULL) + return; + + yyensure_buffer_stack(); + + /* This block is copied from yy_switch_to_buffer. */ + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if (YY_CURRENT_BUFFER) + (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from yy_switch_to_buffer. */ + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void yypop_buffer_state (void) +{ + if (!YY_CURRENT_BUFFER) + return; + + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + if ((yy_buffer_stack_top) > 0) + --(yy_buffer_stack_top); + + if (YY_CURRENT_BUFFER) { + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +static void yyensure_buffer_stack (void) +{ + int num_to_alloc; + + if (!(yy_buffer_stack)) { + + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; + (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc + (num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ + + /* Increase the buffer to prepare for a possible push. */ + int grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc + ((yy_buffer_stack), + num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +/** Setup the input buffer state to scan directly from a user-specified character buffer. + * @param base the character buffer + * @param size the size in bytes of the character buffer + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) +{ + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return 0; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); + + b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = 0; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer(b ); + + return b; +} + +/** Setup the input buffer state to scan a string. The next call to yylex() will + * scan from a @e copy of @a str. + * @param yystr a NUL-terminated string to scan + * + * @return the newly allocated buffer state object. + * @note If you want to scan bytes that may contain NUL values, then use + * yy_scan_bytes() instead. + */ +YY_BUFFER_STATE yy_scan_string (yyconst char * yystr ) +{ + + return yy_scan_bytes(yystr,strlen(yystr) ); +} + +/** Setup the input buffer state to scan the given bytes. The next call to yylex() will + * scan from a @e copy of @a bytes. + * @param bytes the byte buffer to scan + * @param len the number of bytes in the buffer pointed to by @a bytes. + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, int _yybytes_len ) +{ + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + int i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = _yybytes_len + 2; + buf = (char *) yyalloc(n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); + + for ( i = 0; i < _yybytes_len; ++i ) + buf[i] = yybytes[i]; + + buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer(buf,n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; +} + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +static void yy_fatal_error (yyconst char* msg ) +{ + (void) fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + yytext[yyleng] = (yy_hold_char); \ + (yy_c_buf_p) = yytext + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + yyleng = yyless_macro_arg; \ + } \ + while ( 0 ) + +/* Accessor methods (get/set functions) to struct members. */ + +/** Get the current line number. + * + */ +int yyget_lineno (void) +{ + + return yylineno; +} + +/** Get the input stream. + * + */ +FILE *yyget_in (void) +{ + return yyin; +} + +/** Get the output stream. + * + */ +FILE *yyget_out (void) +{ + return yyout; +} + +/** Get the length of the current token. + * + */ +int yyget_leng (void) +{ + return yyleng; +} + +/** Get the current token. + * + */ + +char *yyget_text (void) +{ + return yytext; +} + +/** Set the current line number. + * @param line_number + * + */ +void yyset_lineno (int line_number ) +{ + + yylineno = line_number; +} + +/** Set the input stream. This does not discard the current + * input buffer. + * @param in_str A readable stream. + * + * @see yy_switch_to_buffer + */ +void yyset_in (FILE * in_str ) +{ + yyin = in_str ; +} + +void yyset_out (FILE * out_str ) +{ + yyout = out_str ; +} + +int yyget_debug (void) +{ + return yy_flex_debug; +} + +void yyset_debug (int bdebug ) +{ + yy_flex_debug = bdebug ; +} + +static int yy_init_globals (void) +{ + /* Initialization is the same as for the non-reentrant scanner. + * This function is called from yylex_destroy(), so don't allocate here. + */ + + /* We do not touch yylineno unless the option is enabled. */ + yylineno = 1; + + (yy_buffer_stack) = 0; + (yy_buffer_stack_top) = 0; + (yy_buffer_stack_max) = 0; + (yy_c_buf_p) = (char *) 0; + (yy_init) = 0; + (yy_start) = 0; + +/* Defined in main.c */ +#ifdef YY_STDINIT + yyin = stdin; + yyout = stdout; +#else + yyin = (FILE *) 0; + yyout = (FILE *) 0; +#endif + + /* For future reference: Set errno on error, since we are called by + * yylex_init() + */ + return 0; +} + +/* yylex_destroy is for both reentrant and non-reentrant scanners. */ +int yylex_destroy (void) +{ + + /* Pop the buffer stack, destroying each element. */ + while(YY_CURRENT_BUFFER){ + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + yypop_buffer_state(); + } + + /* Destroy the stack itself. */ + yyfree((yy_buffer_stack) ); + (yy_buffer_stack) = NULL; + + /* Reset the globals. This is important in a non-reentrant scanner so the next time + * yylex() is called, initialization will occur. */ + yy_init_globals( ); + + return 0; +} + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) +{ + register int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * s ) +{ + register int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; +} +#endif + +void *yyalloc (yy_size_t size ) +{ + return (void *) malloc( size ); +} + +void *yyrealloc (void * ptr, yy_size_t size ) +{ + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); +} + +void yyfree (void * ptr ) +{ + free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#line 293 "xas_lex.lex" diff --git a/xcpu/memory.c b/xcpu/memory.c new file mode 100644 index 0000000..fae6540 --- /dev/null +++ b/xcpu/memory.c @@ -0,0 +1,455 @@ +/* memory.c + * This file is a part of decomp - a disassembler. Here are the memory + * allocation and deallocation routines. + * + * Copyright (C) 2001 Jonathan duSaint <dusaint@earthlink.net> + * + * The memory allocation is fairly routine. The justification for not + * using malloc as is is primarily a speed issue. Secondary is the + * fact that this organization allows sloppy memory accounting in the + * main program. When disassembling a file, there will be a lot of + * allocations and deallocations of small, similarly-sized blocks of + * memory (i.e. INSN). By not releasing the memory each time a section + * read is completed, it allows the same block to be reused for the + * next section. + * The mechanism of allocation is that large, CHUNK_SIZE sized blocks + * of memory are requested from the system. They are kept track of + * by overlaying the first sizeof (sys_block) bytes with a + * struct sys_block and having the next pointer point to the next + * block, or being NULL. That way, when xfree is called, the linked + * list, kept in the variable memory, is walked and each node is free'd. + * Each block is then carved up into smaller blocks. Each of these + * smaller blocks has the first sizeof (mem_header) bytes overlaid + * by a struct mem_header. Then, if one of the smaller blocks is needed, + * the struct mem_header is placed on the alloc list. If, relative + * to the size of the memory request, it is fairly large, it is + * partitioned into two blocks. One is placed on the alloc list and + * returned to the caller; the other is placed on the free list. Then, + * as space is needed, adjacent free blocks are merged to create larger + * blocks. If the global variable memory_trace is non-zero, then + * messages are printed to stdout as important/interesting events + * take place. Also provided is the function void mem_trace (void), + * which prints the alloc list, the free list, and the sys list. + * + * Started around 15 October 2001. + */ + + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +#include "xas.h" + + +/* the default amount of memory to request from the system each time */ +#define DEFAULT_CHUNK_SIZE 2048 + +/* the minimum size of housekeeping info in a sys block */ +#define ALLOC_ADJUST (sizeof (struct sys_block) + sizeof (struct mem_header)) + +/* the smallest amount which will be returned */ +#define MIN_ALLOC_AMOUNT 8 + +/* the largest amount that can be handled at one time */ +#define MAX_ALLOC_AMOUNT (chunk_size - ALLOC_ADJUST) + + +/* header for a block of memory - this is what is referenced on + both the alloc and free lists */ +struct mem_header { + /* size of the chunk, not including the header */ + unsigned long size; + struct mem_header *next; +} __attribute__ ((packed)); + + +/* header for the block returned by a call to malloc(3) */ +struct sys_block { + struct sys_block *next; +} __attribute__ ((packed)); + + +/* the free list */ +static struct mem_header *free_list = NULL; + +/* the alloc list */ +static struct mem_header *alloc_list = NULL; + +/* the list of malloc'ed blocks */ +static struct sys_block *memory; + + +/* info used for keeping stats */ + +/* the total memory alloc'ed from the OS */ +static unsigned long total_alloced; + +/* the number of calls to xmalloc */ +static unsigned long xmalloc_calls; + +/* the number of bytes returned by xmalloc */ +static unsigned long xmalloc_bytes; + +/* the number of bytes free'd by xfree */ +static unsigned long xfree_bytes; + + +/* the allocation size for memory chunks */ +static unsigned long chunk_size; + +/* whether or not to print status messages */ +volatile int memory_trace = 0; + +/* whether or not memory is fragmented */ +static int free_list_needs_compacting = 0; + +/* forward declaration needed for mem_trace */ +void compact_free_list (void); + + +/* mem_trace + * Called in case something bad has happened. Prints the contents + * of the alloc list, the free list, and the sys list. + */ +void +mem_trace (void) +{ + struct mem_header *tmp; + struct sys_block *sys; + +/* if (free_list_needs_compacting) */ +/* compact_free_list (); */ + + fprintf (stderr, "alloc list:\n"); + tmp = alloc_list; + while (tmp != NULL) + { + fprintf (stderr, " %p->(%p, %ld)\n", tmp, tmp->next, tmp->size); + tmp = tmp->next; + } + + fprintf (stderr, "free list (%d):\n", free_list_needs_compacting); + tmp = free_list; + while (tmp != NULL) + { + fprintf (stderr, " %p->(%p, %ld)\n", tmp, tmp->next, tmp->size); + tmp = tmp->next; + } + + sys = memory; + fprintf (stderr, "system memory:\n"); + while (sys != NULL) + { + fprintf (stderr, " %p - %p ->(%p)\n", sys, sys + chunk_size, + sys->next); + sys = sys->next; + } +} + + +/* free_list_insert_sorted + * Insert NEW onto the free list, sorted numerically by address. + */ +void +free_list_insert_sorted (struct mem_header *new) +{ + struct mem_header *free_tmp = free_list; + + /* check for empty list */ + if (free_list == NULL) + { + free_list = new; + free_list->next = NULL; + return; + } + + /* if NEW is the lowest address */ + if (new < free_tmp) + { + new->next = free_tmp; + free_list = new; + return; + } + + /* general case - find the insertion point */ + while (free_tmp->next != NULL) + { + if (new < free_tmp->next) break; + free_tmp = free_tmp->next; + } + + new->next = free_tmp->next; + free_tmp->next = new; +} + + +/* compact_free_list + * Defragment memory by merging all adjacent free blocks. + */ +void +compact_free_list (void) +{ + struct mem_header *free_tmp = free_list; + + /* search through the free list for adjacent blocks - if two + are adjacent, then merge them to create a larger block */ + while (free_tmp != NULL) + { + if (free_tmp->next == NULL) break; /* at the end of the list */ + + /* if two free blocks are adjacent */ + if ((void *)free_tmp + free_tmp->size + + sizeof (struct mem_header) == (void *)free_tmp->next) + { + /* merge them */ + free_tmp->size += free_tmp->next->size + + sizeof (struct mem_header); + free_tmp->next = free_tmp->next->next; + + continue; /* try again with this same block */ + } + + free_tmp = free_tmp->next; + } + + free_list_needs_compacting = 0; +} + +/* safe_malloc + * A wrapper around malloc which never returns NULL. + */ +void * +safe_malloc (size_t amount) +{ + void *mem; + + mem = malloc (amount); + + if (mem == NULL) PANIC; /* Houston, we have a problem */ + + /* keep stats */ + total_alloced += amount; + + return (mem); +} + +/* mem_init + * Called when xmalloc is first invoked. Sets up the necessary + * data structures. + */ +void +mem_init (void) +{ + /* set up stats */ + total_alloced = 0; + xmalloc_calls = 0; + xmalloc_bytes = 0; + xfree_bytes = 0; + + /* set up the system list */ + memory = safe_malloc (chunk_size); + memory->next = NULL; + + /* set up the free list */ + free_list = (void *)memory + sizeof (struct sys_block); + free_list->size = chunk_size - sizeof (struct sys_block) + - sizeof (struct mem_header); + free_list->next = NULL; + + if (memory_trace) + printf ("=== alloc'ed block (%p) ===\n", memory); +} + + +/* xmalloc + * Allocate AMOUNT bytes of memory and return it with all of + * its bytes set to 0x00. + */ +void * +xmalloc (size_t amount) +{ + struct mem_header *free_tmp, *last, *ret_block; + struct sys_block *sys_temp; + + chunk_size = DEFAULT_CHUNK_SIZE; + + /* if more than the default chunk size was requested */ + if (amount > MAX_ALLOC_AMOUNT) + chunk_size = amount + ALLOC_ADJUST; + + if (free_list == NULL && alloc_list == NULL) mem_init (); + + /* don't allocate less than MIN_ALLOC_AMOUNT */ + if (amount < MIN_ALLOC_AMOUNT) + amount = MIN_ALLOC_AMOUNT; + + /* keep stats */ + xmalloc_calls++; + xmalloc_bytes += amount; + + + /* loop until a block is found */ + while (1) + { + /* first walk the free list, looking for the correct size */ + last = NULL; + free_tmp = free_list; + + while (free_tmp != NULL) + { + if (free_tmp->size >= amount) goto GOT_FREE_BLOCK; + + last = free_tmp; + free_tmp = free_tmp->next; + } + + + /* didn't find a free block of the right size + - try compacting the free list */ + if (free_list_needs_compacting) + { + compact_free_list (); + continue; + } + + + /* if we got here, then we need to allocate a new chunk */ + + /* find the last chunk */ + sys_temp = memory; + while (sys_temp->next != NULL) sys_temp = sys_temp->next; + + /* alloc a new chunk and save its ptr */ + sys_temp->next = safe_malloc (chunk_size); + sys_temp->next->next = NULL; + + if (memory_trace) + printf ("=== alloc'ed block (%p) ===\n", sys_temp->next); + + /* create the new free block */ + free_tmp = (void *)sys_temp->next + sizeof (struct sys_block); + free_tmp->next = NULL; + free_tmp->size = chunk_size - ALLOC_ADJUST; + + /* put that bad boy in the free list */ + free_list_insert_sorted (free_tmp); + + /* with this new large chunk, try again */ + } + + GOT_FREE_BLOCK: + /* remove the block from the free list */ + if (last == NULL) + free_list = free_tmp->next; + else + last->next = free_tmp->next; + + ret_block = free_tmp; + + + /* shrink the block, if possible */ + if (ret_block->size > amount + sizeof (struct mem_header) + MIN_ALLOC_AMOUNT) + { + struct mem_header *new = (void *)ret_block + + sizeof (struct mem_header) + amount; + + new->size = ret_block->size - sizeof (struct mem_header) - amount; + new->next = NULL; + ret_block->size = amount; + + free_list_insert_sorted (new); + } + + /* put it on the alloc list */ + ret_block->next = alloc_list; + alloc_list = ret_block; + + if (memory_trace) + printf ("=== %d bytes requested (%p) ===\n", amount, + (void *)ret_block + sizeof (struct mem_header)); + + /* and (finally) return the memory */ + memset ((void *)ret_block + sizeof (struct mem_header), 0, amount); + return ((void *)ret_block + sizeof (struct mem_header)); +} + +/* xfree + * Add FREEMEM to the free list. + */ +void +xfree (void *freemem) +{ + struct mem_header *mem; + struct mem_header *tmp = alloc_list, *last = NULL; + + if (freemem == NULL) return; + + mem = freemem - sizeof (struct mem_header); + + /* find FREEMEM on the alloc list */ + while (tmp != NULL) + { + if (tmp == mem) break; + + last = tmp; + tmp = tmp->next; + } + + /* sanity check */ + if (tmp == NULL) + { + fprintf (stderr, + "tried to free block %p (%p->(%p, %ld)) not on alloc list\n", + freemem, mem, mem->next, mem->size); + + if (memory_trace) + mem_trace (); + + exit (EXIT_FAILURE); + } + + /* keep stats */ + xfree_bytes += tmp->size; + + if (memory_trace) + printf ("=== %ld byte block freed (%p) ===\n", tmp->size, freemem); + + /* remove from the alloc list */ + if (last == NULL) + alloc_list = tmp->next; + else + last->next = tmp->next; + + /* put it on the free list */ + free_list_insert_sorted (mem); + free_list_needs_compacting = 1; +} + +/* free_all + * Release all memory back to the OS. + */ +void +free_all (void) +{ + struct sys_block *temp; + + do + { + if (memory_trace) + printf ("=== freeing %p ===\n", memory); + + temp = memory->next; + free (memory); + memory = temp; + } + while (memory != NULL); + + if (memory_trace) + printf ("\n*** Stats ***\n" + " %ld total bytes alloc'ed from the system\n" + " %ld calls to xmalloc\n" + " %ld total bytes alloc'ed\n" + " %ld bytes freed\n", + total_alloced, xmalloc_calls, xmalloc_bytes, xfree_bytes); +} diff --git a/xcpu/test/compare.s b/xcpu/test/compare.s new file mode 100644 index 0000000..291c421 --- /dev/null +++ b/xcpu/test/compare.s @@ -0,0 +1,12 @@ + _include xcpu_asm.h + + cpuop CPU_DEBUG, 1 + + assign A, 5 + assign B, 1 + assign C, 5 + assign D, 9 + compare B + compare C + compare D + jump JUMP_UNCOND diff --git a/xcpu/test/ltest.c b/xcpu/test/ltest.c new file mode 100644 index 0000000..2896dfa --- /dev/null +++ b/xcpu/test/ltest.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "xas.h" + +int line_pos = 0; +int line_no = 1; + +int +main (void) +{ + int lval; + + yyin = fopen ("bootstrap.s", "rt"); + if (yyin == NULL) + { + fprintf (stderr, "Unable to open bootstrap.s\n"); + exit (1); + } + + while ((lval = yylex ()) != 0) + { + switch ((xtoken_t)lval) + { + case FILENAME: + printf ("%4d:%2d:FILENAME:'%s'\n", line_no, line_pos, yytext); + break; + case DIRECTIVE: + printf ("%4d:%2d:DIRECTIVE:'%s'\n", line_no, line_pos, yytext); + break; + case INSTRUCTION: + printf ("%4d:%2d:INSTRUCTION:'%s'\n", line_no, line_pos, yytext); + break; + case ID: + printf ("%4d:%2d:ID:'%s'\n", line_no, line_pos, yytext); + break; + case NUMBER: + printf ("%4d:%2d:NUMBER:'%s'\n", line_no, line_pos, yytext); + break; + case HEXNUMBER: + printf ("%4d:%2d:HEXNUMBER:'%s'\n", line_no, line_pos, yytext); + break; + default: + printf ("%4d:%2d:??%d??:'%s'\n", line_no, line_pos, lval, yytext); + } + } + + fclose (yyin); + + return 0; +} diff --git a/xcpu/xas.c b/xcpu/xas.c new file mode 100644 index 0000000..ee06002 --- /dev/null +++ b/xcpu/xas.c @@ -0,0 +1,807 @@ +/* xas.c + * Assembler for XCPU + * + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <getopt.h> + +#include "xas.h" + +#define PROGRAM_NAME "XAS" +#define VERSION "0.9" +#define AUTHOR "Jonathan duSaint" +#define AUTHOR_EMAIL "jon@rockgeeks.net" + +#define YY_BUFFER_STATE struct yy_buffer_state * +#define YY_BUF_SIZE 16384 +YY_BUFFER_STATE yy_create_buffer (FILE *file, int size); +void yypush_buffer_state (YY_BUFFER_STATE buffer); +void yypop_buffer_state (void); + + +int line_pos = 0; +int line_no = 1; +int line_col = 0; +char *current_file = NULL; + + +int assemble_file (char *filename); + + +FILE *out; +uint32_t load_address; +uint32_t text_start; +uint32_t nbytes_out; + +char *pname; +char *version; +char *author; +char *author_email; + +int debug = 0; +int verbose = 0; +int loud = 0; + + +hash_t symtab; + + +void +panic (long line, char *file) +{ + fprintf (stderr, "%s: PANIC at %ld of %s: %s\n", pname, line, file, + strerror (errno)); + abort (); +} + + +void +print_version (void) +{ + printf ("%s %s\n" + "Copyright (C) 2006 %s <%s>\n" + "%s is free software under the terms of the GNU General Public\n" + "License version 2; see the file COPYING in the source\n" + "distribution for details. This software comes with ABSOLUTELY\n" + "NO WARRANTY.\n", + pname, version, author, author_email, pname); +} + +void +help (void) +{ + printf ( +"Usage: xas [options] file\n" +"Assembler for XCPU\n" +"\n" +"Options:\n" +" -h, --help display this help and exit\n" +" -v, --version output version information and exit\n" +" -V, --verbose print status information while running\n" +" -L, --LOUD print even more information - probably only useful\n" +" for debugging XAS\n" +" -a<address>, --address=<address> specify load address, overriding\n" +" default and any specified in file\n" +" -D<macro>=<val> define a <macro> with <val> as its value\n" +" -g, --debug include debug information in the object file\n" +" -o <file>, --output=<file> specify name of output file\n"); +} + + +#define byte_p(n) ((uint8_t *)(&(n))) + +uint32_t +bswap4 (uint32_t in) +{ + uint32_t sw; + + *(byte_p (sw) + 0) = *(byte_p (in) + 3); + *(byte_p (sw) + 1) = *(byte_p (in) + 2); + *(byte_p (sw) + 2) = *(byte_p (in) + 1); + *(byte_p (sw) + 3) = *(byte_p (in) + 0); + + return sw; +} + +void +yyerror (const char *err) +{ + fprintf (stderr, "%s:%d.%d: %s\n", current_file, line_no, line_col, err); +} + +void +write_header (void) +{ + uint32_t data; + + /* file header */ + fwrite ("XCPU", 1, 4, out); + + /* offset of start of text section */ + data = bswap4 (text_start); + fwrite (&data, 1, 4, out); + + /* text size (will fill in later) */ + data = 0; + fwrite (&data, 1, 4, out); + + /* load address */ + data = bswap4 (load_address); + fwrite (&data, 1, 4, out); +} + +void +finish_header (void) +{ + uint32_t data; + + /* align text to 4 byte boundary */ + if (nbytes_out % 4) + { + uint8_t buf[3] = { 0, 0, 0 }; + uint32_t rem = 4 * ((nbytes_out + 3) >> 2) - nbytes_out; + + fseek (out, 0, SEEK_END); + fwrite (&buf, 1, rem, out); + nbytes_out += rem; + } + + /* write text size now that it's known */ + fseek (out, 8, SEEK_SET); + data = bswap4 (nbytes_out); + fwrite (&data, 1, 4, out); +} + +int +assemble_file (char *filename) +{ + int rv; + FILE *in; + YY_BUFFER_STATE b; + int line_pos_save = line_pos; + int line_no_save = line_no; + int line_col_save = line_col; + char *current_file_save = current_file; + + current_file = filename; + + in = fopen (filename, "rt"); + if (in == NULL) + { + line_pos = line_pos_save; + line_no = line_no_save; + line_col = line_col_save; + current_file = current_file_save; + + fprintf (stderr, "%s:%d.%d: unable to open included file `%s'\n", + current_file, line_no, line_col, filename); + + return 1; + } + + b = yy_create_buffer (in, YY_BUF_SIZE); + + yypush_buffer_state (b); + + rv = yyparse (); + + yypop_buffer_state (); + + fclose (in); + + line_pos = line_pos_save; + line_no = line_no_save; + line_col = line_col_save; + current_file = current_file_save; + + return rv; +} + +uint8_t +regcodes (char reg) +{ + uint8_t code; + + switch (reg) + { + case 'A': + code = 1; + break; + case 'B': + code = 2; + break; + case 'C': + code = 4; + break; + case 'D': + code = 8; + break; + default: + code = 0; + } + + return code; +} + +enum opcode_names { + INS_NOP, INS_JUMP, INS_PUSH, INS_POP, + INS_MEMOP, INS_CPUOP, INS_ASSIGN, INS_ADD, + INS_SHIFT, INS_AND, INS_OR, INS_NOT, + INS_MOVE, INS_COMPARE, INS_HALT, INS_CONFIG +}; + +uint8_t opcodes[] = { + 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, + 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0 +}; + +void +ins_config (uint32_t arg) +{ + uint8_t op = opcodes[INS_CONFIG]; + + op |= (uint8_t)(arg & 0xf); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_jump (uint32_t arg) +{ + uint8_t op = opcodes[INS_JUMP]; + + op |= (uint8_t)(arg & 0xf); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_push (char reg) +{ + uint8_t op = opcodes[INS_PUSH]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_pop (char reg) +{ + uint8_t op = opcodes[INS_POP]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_memop (char reg) +{ + uint8_t op = opcodes[INS_MEMOP]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_cpuop (uint32_t val, uint32_t bit) +{ + uint8_t op = opcodes[INS_CPUOP]; + + op |= (((uint8_t)bit << 3) & 0x8) | ((uint8_t)val & 0x7); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_assign (char reg, uint32_t arg) +{ + uint8_t op[5]; + + op[0] = opcodes[INS_ASSIGN] | regcodes (reg); + op[1] = *(byte_p (arg) + 3); + op[2] = *(byte_p (arg) + 2); + op[3] = *(byte_p (arg) + 1); + op[4] = *(byte_p (arg) + 0); + + fwrite (&op, 1, 5, out); + + nbytes_out += 5; +} + +void +ins_add (char reg) +{ + uint8_t op = opcodes[INS_ADD]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_shift (char reg) +{ + uint8_t op = opcodes[INS_SHIFT]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_and (char reg) +{ + uint8_t op = opcodes[INS_AND]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_or (char reg) +{ + uint8_t op = opcodes[INS_OR]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_not (char reg) +{ + uint8_t op = opcodes[INS_NOT]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_move (char reg) +{ + uint8_t op = opcodes[INS_MOVE]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_compare (char reg) +{ + uint8_t op = opcodes[INS_COMPARE]; + + op |= regcodes (reg); + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_halt (void) +{ + uint8_t op = opcodes[INS_HALT]; + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + +void +ins_nop (void) +{ + uint8_t op = opcodes[INS_NOP]; + + fwrite (&op, 1, 1, out); + + nbytes_out++; +} + + +void +dir_include (char *filename) +{ + int rv; + + if (loud) + printf ("Including '%s'\n", filename); + + rv = assemble_file (filename); + + if (loud) + printf ("Out of included '%s'\n", filename); +} + +void +dir_define (char *id, uint32_t arg) +{ + int exists = 0; + struct symbol_data *symdata; + + if ((symdata = hash_get (symtab, id)) != NULL) + exists = 1; + else + { + symdata = xmalloc (sizeof (struct symbol_data)); + symdata->location_data = NULL; + symdata->is_resolved = 0; + } + + if (symdata->is_resolved == 0) + { + symdata->value = arg; + symdata->is_resolved = 1; + } + + if (verbose) + printf ("Creating define `%s' with value %u\n", id, arg); + + if (!exists) + hash_add (symtab, id, symdata); +} + +void +dir_label (char *id) +{ + uint32_t loc; + struct symbol_data *symdata; + int exists = 0; + + /* get label location */ + loc = nbytes_out + load_address; + + if ((symdata = hash_get (symtab, id)) != NULL) + { + /* if this label exists, it was used as a forward ref */ + exists = 1; + } + else + { + symdata = xmalloc (sizeof (struct symbol_data)); + symdata->location_data = NULL; + } + + symdata->value = loc; + symdata->is_resolved = 1; + + if (verbose) + printf ("Creating label `%s' at %08x\n", id, loc); + + if (!exists) + hash_add (symtab, id, symdata); +} + +void +dir_immed_string (char *str) +{ + int s = 0, e = 0, n = strlen (str);; + char *strout; + uint32_t len; + + strout = xmalloc (n + 1); + + while (e < n) + { + if (str[e] == '\\') + { + switch (str[++e]) + { + case 'n': + strout[s++] = '\n'; + break; + case 't': + strout[s++] = '\t'; + break; + case '\\': + strout[s++] = '\\'; + break; + default: + strout[s++] = '?'; + } + e++; + } + else + strout[s++] = str[e++]; + } + + len = (uint32_t)e; + + fwrite (strout, 1, len, out); + + xfree (strout); + + nbytes_out += len; +} + +void +dir_immed_bytes (uint32_t nbytes) +{ + uint32_t k; + uint8_t zero = '\0'; + + for (k = 0; k < nbytes; k++) + fwrite (&zero, 1, 1, out); + + nbytes_out += nbytes; +} + +uint32_t +resolve_identifier (char *id) +{ + struct symbol_data *symdata; + + symdata = hash_get (symtab, id); + + if (symdata == NULL) + { + /* symbol doesn't exist - create unresolved symbol */ + symdata = xmalloc (sizeof (struct symbol_data)); + symdata->value = 0; + symdata->is_resolved = 0; + symdata->location_data = xmalloc (sizeof (struct location_data)); + symdata->location_data->next = NULL; + symdata->location_data->location = ftell (out); + symdata->location_data->file = xmalloc (strlen (current_file) + 1); + strcpy (symdata->location_data->file, current_file); + symdata->location_data->line_no = line_no; + symdata->location_data->line_col = line_col; + + hash_add (symtab, id, symdata); + + if (verbose) + printf ("Unresolved symbol `%s' created\n", id); + } + else if (symdata->is_resolved == 0) + { + /* symbol exists, but is unresolved - add this ref to the locations */ + struct location_data *p; + + p = symdata->location_data; + if (p == NULL) + { + fprintf (stderr, "%s internal: symdata exists, is_resolved == 0, but location_data == NULL for ID `%s'\n", pname, id); + symdata->location_data = xmalloc (sizeof (struct location_data)); + symdata->location_data->next = NULL; + p = symdata->location_data; + } + + while (p->next != NULL) p = p->next; + + p->next = xmalloc (sizeof (struct location_data)); + p->next->next = NULL; + p->next->location = ftell (out); + p->next->file = xmalloc (strlen (current_file) + 1); + strcpy (p->next->file, current_file); + p->next->line_no = line_no; + p->next->line_col = line_col; + + if (verbose) + printf ("Unresolved symbol `%s' referenced\n", id); + } + else if (verbose) + printf ("Symbol `%s' (%u) referenced\n", id, symdata->value); + + return symdata->value; +} + +void +resolve_symbols (void) +{ + char *id; + struct symbol_data *symdata; + struct location_data *locdata; + + if (loud) + printf ("Resolving symbols\n"); + + hash_walk (NULL); + + while ((id = hash_walk (symtab)) != HASH_END) + { + uint32_t val; + + symdata = hash_get (symtab, id); + + locdata = symdata->location_data; + + val = bswap4 (symdata->value); + + while (locdata != NULL) + { + struct location_data *ldtmp; + + if (verbose) + printf ("Writing symbol `%s' data (%u) to %ld\n", + id, bswap4 (val), locdata->location + 1); + + if (symdata->is_resolved == 0) + fprintf (stderr, "%s:%d.%d: unresolved symbol `%s'\n", + locdata->file, locdata->line_no, locdata->line_col, id); + else + { + fseek (out, locdata->location + 1, SEEK_SET); + fwrite (&val, 4, 1, out); + } + + ldtmp = locdata; + locdata = locdata->next; + + xfree (ldtmp->file); + xfree (ldtmp); + } + + /* need to delete the symbol due to a bug in hash_walk */ + hash_remove (symtab, id); + } +} + + +int +main (int argc, char *argv[]) +{ + char *output_file = NULL, *input_file; + int isok; + int opt, index; + FILE *realout; + struct option options[] = { + { "address", required_argument, NULL, 'a' }, + { "", required_argument, NULL, 'D' }, + { "debug", no_argument, NULL, 'g' }, + { "output", required_argument, NULL, 'o' }, + { "verbose", no_argument, NULL, 'V' }, + { "LOUD", no_argument, NULL, 'L' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'v' }, + { 0, 0, 0, 0 }, + }; + + pname = PROGRAM_NAME; + version = VERSION; + author = AUTHOR; + author_email = AUTHOR_EMAIL; + + load_address = 0x110; /* start of memory with normal bootstrap */ + text_start = 0x10; /* file offset without debug info */ + + opterr = 0; + + symtab = hash_new (0); + + while (1) + { + opt = getopt_long (argc, argv, "a:D:go:VLhv", options, &index); + + if (opt == EOF) break; + + switch (opt) + { + case 'a': + load_address = strtol (optarg, NULL, 0); + break; + case 'D': + /* parse define */ + break; + case 'g': + debug = 1; + break; + case 'o': + output_file = xmalloc (strlen (optarg) + 1); + strcpy (output_file, optarg); + break; + case 'V': + verbose = 1; + break; + case 'L': + loud = 1; + break; + case 'h': + help (); + return 0; + case 'v': + print_version (); + return 0; + case ':': + exit (1); + case '?': + fprintf (stderr, "%s: unknown option character `%c'\n", + pname, optopt); + break; + default: + fprintf (stderr, "%s: ?? getopt returned character code 0x%x ??\n", + pname, opt); + } + } + + input_file = argv[optind]; + + + if (output_file == NULL) + { + size_t nchar = strlen (input_file); + + output_file = xmalloc (nchar + 5); + if (input_file[nchar-2] == '.' && input_file[nchar-1] == 's') nchar -= 2; + + strncpy (output_file, input_file, nchar); + strcat (output_file, ".xcpu"); + } + + + nbytes_out = 0; /* position info within output */ + + errno = 0; + out = tmpfile (); + if (out == NULL) + { + fprintf (stderr, "%s: unable to create temporary output file: %s\n", + pname, strerror (errno)); + exit (1); + } + + write_header (); + isok = assemble_file (input_file); + resolve_symbols (); + finish_header (); + + /* now move tmpfile to outfile */ + errno = 0; + realout = fopen (output_file, "wb"); + if (realout == NULL) + { + fprintf (stderr, "%s: unable to create output file `%s': %s\n", + pname, output_file, strerror (errno)); + exit (1); + } + + fseek (out, 0, SEEK_SET); + + while (!feof (out)) + { + uint8_t buf; + + fread (&buf, 1, 1, out); + fwrite (&buf, 1, 1, realout); + } + + fclose (realout); + fclose (out); + xfree (output_file); + hash_delete (symtab); + free_all (); + + return 0; +} diff --git a/xcpu/xas.h b/xcpu/xas.h new file mode 100644 index 0000000..03c4d22 --- /dev/null +++ b/xcpu/xas.h @@ -0,0 +1,106 @@ +/* XAS.H + * Global definitions for XAS, an assembler for XCPU. + * + */ + +#ifndef _XAS_H +#define _XAS_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <math.h> +#include <errno.h> + +#include "xas_parse.tab.h" + +/* something really bad just happened and it's time to bail */ +#define PANIC panic (__LINE__, __FILE__) +void panic (long line, char *file); + +/* an invalid hash key */ +#define HASH_END ((void *)-1) + +/* hash table stuff */ +struct hash_bucket { + struct hash_bucket *next; + char * key; + void *data; +}; + +struct hash_table { + struct hash_bucket **table; + unsigned long size; + unsigned long items; +}; + +typedef struct hash_table *hash_t; + +/* hash.c */ +hash_t hash_new (unsigned long size); +int hash_add (hash_t table, char *key, void *data); +void *hash_get (hash_t table, char *key); +void *hash_remove (hash_t table, char *key); +void hash_delete (hash_t table); +void hash_print (hash_t table); +char *hash_walk (hash_t table); + + +struct location_data { + struct location_data *next; + long location; + char *file; + int line_no; + int line_col; +}; + +struct symbol_data { + uint32_t value; + int is_resolved; + struct location_data *location_data; +}; + + + +extern int line_pos; +extern int line_no; +extern int line_col; + +void yyerror (const char *); +int yyparse (void); + +extern FILE *yyin; + +void ins_config (uint32_t arg); +void ins_jump (uint32_t arg); +void ins_push (char reg); +void ins_pop (char reg); +void ins_memop (char reg); +void ins_cpuop (uint32_t bit, uint32_t val); +void ins_assign (char reg, uint32_t arg); +void ins_add (char reg); +void ins_shift (char reg); +void ins_and (char reg); +void ins_or (char reg); +void ins_not (char reg); +void ins_move (char reg); +void ins_compare (char reg); +void ins_halt (void); +void ins_nop (void); + +void dir_include (char *filename); +void dir_define (char *id, uint32_t arg); +void dir_label (char *id); +void dir_immed_string (char *str); +void dir_immed_bytes (uint32_t nbytes); + +uint32_t resolve_identifier (char *id); + +/* memory.c */ +void *xmalloc (size_t amount); +void xfree (void *mem); +void free_all (void); +void mem_trace (void); + +#endif /* _XAS_H */ diff --git a/xcpu/xas_lex.lex b/xcpu/xas_lex.lex new file mode 100644 index 0000000..23653d7 --- /dev/null +++ b/xcpu/xas_lex.lex @@ -0,0 +1,292 @@ +/* xas_lex.lex + * Lexical analyzer definition file for XAS, an assembler for XCPU. + * + */ + +%{ +#include "xas.h" +%} + +%option 8bit bison-bridge bison-locations +%option warn nodefault +%option yylineno +%option noyywrap + +ALNUM [A-Za-z0-9_] +DIGIT [0-9] +BIT [01] +ALPHA [A-Za-z_] +HEX [0-9A-Fa-f] +REG [ABCD] + +%% + +{BIT} { + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + yylval->val = strtoul (yytext, NULL, 2); + + return BIT; +} + +{DIGIT}+ { + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + yylval->val = strtoul (yytext, NULL, 0); + + return NUMBER; +} + +"0x"{HEX}+ { + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + yylval->val = strtoul (yytext, NULL, 16); + + return NUMBER; +} + +"'"."'" { + line_pos++; + line_col += yyleng; + + yylval->character = yytext[1]; + + return CHARACTER; +} + +"\"".*"\"" { + line_pos++; + line_col += yyleng; + + yytext[yyleng-1] = 0; + + yylval->id = xmalloc (yyleng - 1); + strncpy (yylval->string, yytext + 1, yyleng - 2); + + return STRING; +} + +"'\\n'" { + line_pos++; + line_col += yyleng; + + yylval->character = '\n'; + + return CHARACTER; +} + +"'\\\\'" { + line_pos++; + line_col += yyleng; + + yylval->character = '\\'; + + return CHARACTER; +} + +{REG} { + line_pos++; + line_col += yyleng; + + yylval->reg = yytext[0]; + + return REGISTER; +} + +"nop" { + line_pos++; + line_col += yyleng; + + return NOP; +} + +"jump" { + line_pos++; + line_col += yyleng; + + return JUMP; +} + +"push" { + line_pos++; + line_col += yyleng; + + return PUSH; +} + +"pop" { + line_pos++; + line_col += yyleng; + + return POP; +} + +"memop" { + line_pos++; + line_col += yyleng; + + return MEMOP; +} + +"cpuop" { + line_pos++; + line_col += yyleng; + + return CPUOP; +} + +"assign" { + line_pos++; + line_col += yyleng; + + return ASSIGN; +} + +"add" { + line_pos++; + line_col += yyleng; + + return ADD; +} + +"shift" { + line_pos++; + line_col += yyleng; + + return SHIFT; +} + +"and" { + line_pos++; + line_col += yyleng; + + return AND; +} + +"or" { + line_pos++; + line_col += yyleng; + + return OR; +} + +"not" { + line_pos++; + line_col += yyleng; + + return NOT; +} + +"move" { + line_pos++; + line_col += yyleng; + + return MOVE; +} + +"compare" { + line_pos++; + line_col += yyleng; + + return COMPARE; +} + +"halt" { + line_pos++; + line_col += yyleng; + + return HALT; +} + +"config" { + line_pos++; + line_col += yyleng; + + return CONFIG; +} + +"_include" { + line_pos++; + line_col += yyleng; + + return INCLUDE; +} + +"_define" { + line_pos++; + line_col += yyleng; + + return DEFINE; +} + +"_label" { + line_pos++; + line_col += yyleng; + + return LABEL; +} + +"_immed" { + line_pos++; + line_col += yyleng; + + return IMMED; +} + +{ALPHA}{ALNUM}* { + line_pos++; + line_col += yyleng; + + yytext[yyleng] = 0; + +/* if (line_pos == 1) */ +/* return MACROREF; */ + + yylval->id = xmalloc (yyleng + 1); + strncpy (yylval->id, yytext, yyleng); + + return ID; +} + +{ALNUM}+"."{ALNUM}+ { + line_pos++; + line_col += yyleng; + + yylval->id = xmalloc (yyleng + 1); + strncpy (yylval->id, yytext, yyleng); + + return FILENAME; +} + +";"|"#" { + int c; + while ((c = input ()) != EOF) + { + if (c == '\n') + { + unput (c); + break; + } + } +} + +"\n" { + line_no++; + line_pos = 0; + line_col = 0; +} + +" "|"\t" { + line_col++; +} + +. { + line_col++; + return yytext[0]; +} diff --git a/xcpu/xas_parse.y b/xcpu/xas_parse.y new file mode 100644 index 0000000..d6b2fd2 --- /dev/null +++ b/xcpu/xas_parse.y @@ -0,0 +1,144 @@ +/* xas_parse.y + * Parser description file for XAS, an assembler for XCPU. + * + */ + +%{ +#include "xas.h" + +int yylex (YYSTYPE *lvalp, YYLTYPE *llocp); +%} + +%locations +%pure-parser +%error-verbose + +%union { + char reg; + uint32_t val; + char *id; + char character; + char *string; +} + + +%token <id> ID +%token <val> NUMBER +%token <val> BIT +%token <reg> REGISTER +%token <character> CHARACTER +%token <string> STRING +%token <id> FILENAME + +%token NOP +%token JUMP +%token PUSH +%token POP +%token MEMOP +%token CPUOP +%token ASSIGN +%token ADD +%token SHIFT +%token AND +%token OR +%token NOT +%token MOVE +%token COMPARE +%token HALT +%token CONFIG + +%token INCLUDE +%token DEFINE +%token LABEL +%token IMMED + +/* %token MACRO */ +/* %token ENDMACRO */ +/* %token MACROREF */ + +%left '|' +%left '$' +%left '&' +%left '<' '>' +%left '-' '+' +%left '*' '/' '%' +%left NEG POS +%right '~' +%right '^' + +%type <val> arg +%type <val> expr + +%% + +input: /* empty */ + | input line +; + +line: '\n' + | instruction + | directive + | error { yyclearin; yyerrok; } +/* | macroref */ +; + +instruction: NOP { ins_nop (); } + | JUMP arg { ins_jump ($2); } + | PUSH REGISTER { ins_push ($2); } + | POP REGISTER { ins_pop ($2); } + | MEMOP REGISTER { ins_memop ($2); } + | CPUOP arg ',' arg { ins_cpuop ($2, $4); } + | ASSIGN REGISTER ',' expr { ins_assign ($2, $4); } + | ADD REGISTER { ins_add ($2); } + | SHIFT REGISTER { ins_shift ($2); } + | AND REGISTER { ins_and ($2); } + | OR REGISTER { ins_or ($2); } + | NOT REGISTER { ins_not ($2); } + | MOVE REGISTER { ins_move ($2); } + | COMPARE REGISTER { ins_compare ($2); } + | HALT { ins_halt (); } + | CONFIG arg { ins_config ($2); } +; + +directive: INCLUDE FILENAME { dir_include ($2); } + | DEFINE ID expr { dir_define ($2, $3); } + | LABEL ID { dir_label ($2); } + | IMMED STRING { dir_immed_string ($2); } + | IMMED expr { dir_immed_bytes ($2); } +/* | MACRO ID mparam { dir_begin_ignore (); } */ +/* | ENDMACRO { dir_end_ignore (); } */ +; + +/* macroref: MACROREF mparam { expand_macro ($2); } */ +/* ; */ + +arg: NUMBER { $$ = $1; } + | BIT { $$ = $1; } + | ID { $$ = resolve_identifier ($1); } +; + +expr: NUMBER { $$ = $1; } + | BIT { $$ = $1; } + | ID { $$ = resolve_identifier ($1); } + | CHARACTER { $$ = (uint32_t)$1; } + | expr '|' expr { $$ = $1 | $3; } + | expr '$' expr { $$ = $1 ^ $3; } + | expr '&' expr { $$ = $1 & $3; } + | expr '<' expr { $$ = $1 << $3; } + | expr '>' expr { $$ = $1 >> $3; } + | expr '+' expr { $$ = $1 + $3; } + | expr '-' expr { $$ = $1 - $3; } + | expr '*' expr { $$ = $1 * $3; } + | expr '/' expr { $$ = $1 / $3; } + | expr '%' expr { $$ = $1 % $3; } + | '+' expr %prec POS { $$ = +$2; } + | '-' expr %prec NEG { $$ = -$2; } + | '~' expr { $$ = ~$2; } + | expr '^' expr { $$ = pow ($1, $3); } + | '(' expr ')' { $$ = $2; } +; + +/* mparam: mparam ',' arg */ +/* ; */ + +%% diff --git a/xcpu/xcpu.c b/xcpu/xcpu.c new file mode 100644 index 0000000..e17d3bf --- /dev/null +++ b/xcpu/xcpu.c @@ -0,0 +1,1075 @@ +/* XCPU.c + * Main source file for XCPU. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <errno.h> + +/* memory.c */ +void *xmalloc (size_t amount); +void xfree (void *mem); +void free_all (void); +void mem_trace (void); + +char *disassemble_instruction (uint8_t *mem); +void xcpu_print_cpu_state (void); + +/* something really bad just happened and it's time to bail */ +#define PANIC panic (__LINE__, __FILE__) +void panic (long line, char *file); + + +#define BOOTSTRAP_FILE "bootstrap.xcpu" + +typedef struct { + void (*seek)(uint32_t); + uint32_t (*tell)(void); + uint32_t (*read)(void); + void (*write)(uint32_t); +} _ioport, *ioport; + +static ioport ioports[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; + +static int active_ioport; +static char *dis = NULL; + + +#define N_IOPORTS 8 + + +typedef void (*instruction_function)(uint8_t); +static instruction_function instructions[16]; + + +typedef struct { + uint32_t IP; + uint32_t SP; + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; + uint32_t M; + uint8_t status; + + uint32_t rom_start; + uint32_t rom_size; + + uint32_t memory_start; + uint32_t memory_size; + uint32_t stack_start; + uint32_t stack_size; + + uint32_t ioport_start; + + int halted; +} _XCPU, *XCPU; + +#define XCPU_SIZE sizeof (_XCPU) +#define ROM_SIZE 256 + +#define REG_A 1 +#define REG_B 2 +#define REG_C 4 +#define REG_D 8 + + +#define XCPU_JUMP1 0 +#define XCPU_JUMP2 1 +#define XCPU_MEMOP 2 +#define XCPU_SLR 3 +#define XCPU_SRC 4 +#define XCPU_MOVE 5 +#define XCPU_DEBUG 7 + +#define XCPU_GET_STATUS_BIT(b) ((cpu->status & (1 << (b))) >> (b)) +#define XCPU_SET_STATUS_BIT(b) cpu->status |= 1 << (b) +#define XCPU_CLEAR_STATUS_BIT(b) cpu->status &= ~(1 << (b)) + +#define JUMP_UNCOND 0 +#define JUMP_EQ 1 +#define JUMP_NEQ 2 +#define JUMP_LT 3 +#define JUMP_GT 4 +#define CALL 7 + + + +static FILE *infp; +static XCPU cpu; + +static uint8_t *rom = NULL; +static uint8_t *memory = NULL; + +int icount = 0; + + +void +panic (long line, char *file) +{ + fprintf (stderr, "XCPU: PANIC at %ld of %s: %s\n", line, file, + strerror (errno)); + abort (); +} + + + +void +exception (char *m) +{ + fprintf (stderr, "XCPU: An exception occurred: %s\n", m); + xcpu_print_cpu_state (); +} + + +#define byte_p(n) ((uint8_t *)(&(n))) + +uint32_t +bswap4 (uint32_t in) +{ + uint32_t out; + + *(byte_p (out) + 0) = *(byte_p (in) + 3); + *(byte_p (out) + 1) = *(byte_p (in) + 2); + *(byte_p (out) + 2) = *(byte_p (in) + 1); + *(byte_p (out) + 3) = *(byte_p (in) + 0); + + return out; +} + +void +push_stack (uint32_t arg) +{ + *(uint32_t *)(&memory[cpu->SP - cpu->memory_start]) = bswap4 (arg); + cpu->SP += 4; +} + +uint32_t +pop_stack (void) +{ + uint32_t arg; + + cpu->SP -= 4; + + arg = bswap4 (*(uint32_t *)(&memory[cpu->SP - cpu->memory_start])); + + return arg; +} + +void +in_config (uint8_t ia) +{ + switch (ia) + { + case 0: + /* memory */ + memory = xmalloc (cpu->B); + cpu->memory_start = cpu->A; + cpu->memory_size = cpu->B; + break; + case 1: + /* stack */ + cpu->stack_start = cpu->A; + cpu->stack_size = (cpu->memory_size - cpu->memory_start + - cpu->stack_start); + cpu->SP = cpu->stack_start; + break; + case 2: + /* ioports */ + cpu->ioport_start = cpu->A; + break; + default: + fprintf (stderr, "XCPU: invalid config arg %02x\n", ia); + } +} + + +void +in_jump (uint8_t ia) +{ + uint32_t jump_addr, new_addr; + + new_addr = cpu->IP; + jump_addr = pop_stack (); + + switch (ia) + { + case CALL: + push_stack (cpu->IP); + case JUMP_UNCOND: + new_addr = jump_addr; + break; + case JUMP_EQ: + if (XCPU_GET_STATUS_BIT (XCPU_JUMP1)) + new_addr = jump_addr; + break; + case JUMP_NEQ: + if (!XCPU_GET_STATUS_BIT (XCPU_JUMP1)) + new_addr = jump_addr; + break; + case JUMP_LT: + if (!XCPU_GET_STATUS_BIT (XCPU_JUMP2) + && XCPU_GET_STATUS_BIT (XCPU_JUMP1)) + new_addr = jump_addr; + break; + case JUMP_GT: + if (XCPU_GET_STATUS_BIT (XCPU_JUMP2) + && XCPU_GET_STATUS_BIT (XCPU_JUMP1)) + new_addr = jump_addr; + break; + default: + exception ("Invalid jump type"); + } + + cpu->IP = new_addr; +} + +void +in_push (uint8_t ia) +{ + uint32_t push_val; + + switch (ia) + { + case REG_A: + push_val = cpu->A; + break; + case REG_B: + push_val = cpu->B; + break; + case REG_C: + push_val = cpu->C; + break; + case REG_D: + push_val = cpu->D; + break; + default: + exception ("Invalid register specification"); + return; + } + + push_stack (push_val); +} + +void +in_pop (uint8_t ia) +{ + uint32_t pop_val; + + pop_val = pop_stack (); + + switch (ia) + { + case REG_A: + cpu->A = pop_val; + break; + case REG_B: + cpu->B = pop_val; + break; + case REG_C: + cpu->C = pop_val; + break; + case REG_D: + cpu->D = pop_val; + break; + default: + exception ("Invalid register specification"); + } +} + +void +in_memop (uint8_t ia) +{ + uint32_t addr, val = 0; + + addr = pop_stack (); + + if (XCPU_GET_STATUS_BIT (XCPU_MEMOP)) /* LOAD */ + { + if (addr >= cpu->rom_start && addr < cpu->rom_start + cpu->rom_size) + val = bswap4 (*(uint32_t *)(&rom[addr - cpu->rom_start])); + else if (addr >= cpu->memory_start + && addr < cpu->memory_start + cpu->memory_size) + val = bswap4 (*(uint32_t *)(&memory[addr - cpu->memory_start])); + else if (addr >= cpu->ioport_start && addr < cpu->ioport_start + 12) + { + if (addr == cpu->ioport_start) + val = (uint32_t)active_ioport; + else if (addr == cpu->ioport_start + 4) + { + if (active_ioport >= N_IOPORTS) + exception ("I/O port value greater than available I/O ports"); + else if (ioports[active_ioport] == NULL) + exception ("Selected I/O port does not exist"); + else + val = ioports[active_ioport]->tell (); + } + else if (addr == cpu->ioport_start + 8) + { + if (active_ioport >= N_IOPORTS) + exception ("I/O port value greater than available I/O ports"); + else if (ioports[active_ioport] == NULL) + exception ("Selected I/O port does not exist"); + else + val = ioports[active_ioport]->read (); + } + else + exception ("MEMOP attempted unaligned load from I/O ports"); + } + else + exception ("MEMOP attempted load in unmapped memory"); + + switch (ia) + { + case REG_A: + cpu->A = val; + break; + case REG_B: + cpu->B = val; + break; + case REG_C: + cpu->C = val; + break; + case REG_D: + cpu->D = val; + break; + default: + exception ("Invalid register specification"); + } + } + else /* STORE */ + { + switch (ia) + { + case REG_A: + val = cpu->A; + break; + case REG_B: + val = cpu->B; + break; + case REG_C: + val = cpu->C; + break; + case REG_D: + val = cpu->D; + break; + default: + exception ("Invalid register specification"); + } + + if (addr >= cpu->rom_start && addr < cpu->rom_start + cpu->rom_size) + exception ("MEMOP attempted store in ROM"); + else if (addr >= cpu->memory_start + && addr < cpu->memory_start + cpu->memory_size) + *(uint32_t *)(&memory[addr - cpu->memory_start]) = bswap4 (val); + else if (addr >= cpu->ioport_start && addr < cpu->ioport_start + 12) + { + if (addr == cpu->ioport_start) + { + if (val >= N_IOPORTS) + exception ("I/O port value greater than available I/O ports"); + else if (ioports[val] == NULL) + exception ("Selected I/O port does not exist"); + else + active_ioport = (int)val; + } + else if (addr == cpu->ioport_start + 4) + { + if (active_ioport >= N_IOPORTS) + exception ("I/O port value greater than available I/O ports"); + else if (ioports[active_ioport] == NULL) + exception ("Selected I/O port does not exist"); + else + ioports[active_ioport]->seek (val); + } + else if (addr == cpu->ioport_start + 8) + { + if (active_ioport >= N_IOPORTS) + exception ("I/O port value greater than available I/O ports"); + else if (ioports[active_ioport] == NULL) + exception ("Selected I/O port does not exist"); + else + ioports[active_ioport]->write (val); + } + else + exception ("MEMOP attempted unaligned store to I/O ports"); + } + else + exception ("MEMOP attempted store in unmapped memory"); + } +} + + +void +in_cpuop (uint8_t ia) +{ + uint8_t mask = 0, bit; + + bit = (ia & 0x8) >> 3; + mask = 1 << (ia & 0x7); + + if (bit) + cpu->status |= mask; + else + cpu->status &= ~mask; +} + +void +in_assign (uint8_t ia) +{ + uint32_t val; + + if (cpu->IP > cpu->rom_start && cpu->IP < cpu->rom_start + cpu->rom_size) + val = bswap4 (*(uint32_t *)(&rom[cpu->IP - cpu->rom_start])); + else if (cpu->IP > cpu->memory_start + && cpu->IP < cpu->memory_start + cpu->memory_size) + val = bswap4 (*(uint32_t *)(&memory[cpu->IP - cpu->memory_start])); + else + { + fprintf (stderr, "XCPU: IP points to lala land for assign\n"); + return; + } + + cpu->IP += 4; + + switch (ia) + { + case REG_A: + cpu->A = val; + break; + case REG_B: + cpu->B = val; + break; + case REG_C: + cpu->C = val; + break; + case REG_D: + cpu->D = val; + break; + default: + exception ("Invalid register specification"); + } +} + +void +in_add (uint8_t ia) +{ + switch (ia) + { + case REG_A: + cpu->A += cpu->A; + break; + case REG_B: + cpu->B += cpu->A; + break; + case REG_C: + cpu->C += cpu->A; + break; + case REG_D: + cpu->D += cpu->A; + break; + default: + exception ("Invalid register specification"); + } +} + +void +in_shift (uint8_t ia) +{ + uint32_t val, sh; + + sh = cpu->A; + + switch (ia) + { + case REG_A: + val = cpu->A; + break; + case REG_B: + val = cpu->B; + break; + case REG_C: + val = cpu->C; + break; + case REG_D: + val = cpu->D; + break; + default: + val = 0; + exception ("Invalid register specification"); + } + + if (XCPU_GET_STATUS_BIT (XCPU_SLR)) + { + if (XCPU_GET_STATUS_BIT (XCPU_SRC)) + /* right circular */ + val = val >> sh | val << (32 - sh); + else + /* right regular */ + val >>= sh; + } + else + { + if (XCPU_GET_STATUS_BIT (XCPU_SRC)) + /* left circular */ + val = val << sh | val >> (32 - sh); + else + /* left regular */ + val <<= sh; + } + + switch (ia) + { + case REG_A: + cpu->A = val; + break; + case REG_B: + cpu->B = val; + break; + case REG_C: + cpu->C = val; + break; + case REG_D: + cpu->D = val; + break; + default: + exception ("Invalid register specification"); + } +} + +void +in_and (uint8_t ia) +{ + switch (ia) + { + case REG_A: + cpu->A &= cpu->A; + break; + case REG_B: + cpu->B &= cpu->A; + break; + case REG_C: + cpu->C &= cpu->A; + break; + case REG_D: + cpu->D &= cpu->A; + break; + default: + exception ("Invalid register specification"); + } +} + +void +in_or (uint8_t ia) +{ + switch (ia) + { + case REG_A: + cpu->A |= cpu->A; + break; + case REG_B: + cpu->B |= cpu->A; + break; + case REG_C: + cpu->C |= cpu->A; + break; + case REG_D: + cpu->D |= cpu->A; + break; + default: + exception ("Invalid register specification"); + } +} + +void +in_not (uint8_t ia) +{ + switch (ia) + { + case REG_A: + cpu->A = ~cpu->A; + break; + case REG_B: + cpu->B = ~cpu->B; + break; + case REG_C: + cpu->C = ~cpu->C; + break; + case REG_D: + cpu->D = ~cpu->D; + break; + default: + exception ("Invalid register specification"); + } +} + +void +in_move (uint8_t ia) +{ + if (XCPU_GET_STATUS_BIT (XCPU_MOVE)) + { + /* moving from M */ + switch (ia) + { + case REG_A: + cpu->A = cpu->M; + break; + case REG_B: + cpu->B = cpu->M; + break; + case REG_C: + cpu->C = cpu->M; + break; + case REG_D: + cpu->D = cpu->M; + break; + default: + exception ("Invalid register specification"); + } + } + else + { + /* moving to M */ + switch (ia) + { + case REG_A: + cpu->M = cpu->A; + break; + case REG_B: + cpu->M = cpu->B; + break; + case REG_C: + cpu->M = cpu->C; + break; + case REG_D: + cpu->M = cpu->D; + break; + default: + exception ("Invalid register specification"); + } + } +} + +void +in_compare (uint8_t ia) +{ + uint32_t val; + + switch (ia) + { + case REG_A: + val = cpu->A; + break; + case REG_B: + val = cpu->B; + break; + case REG_C: + val = cpu->C; + break; + case REG_D: + val = cpu->D; + break; + default: + val = 0; + exception ("Invalid register specification"); + } + + XCPU_CLEAR_STATUS_BIT (XCPU_JUMP1); + XCPU_CLEAR_STATUS_BIT (XCPU_JUMP2); + + if (val == cpu->A) + { + XCPU_CLEAR_STATUS_BIT (XCPU_JUMP1); + } + else if (val < cpu->A) + { + XCPU_CLEAR_STATUS_BIT (XCPU_JUMP2); + XCPU_SET_STATUS_BIT (XCPU_JUMP1); + } + else if (val > cpu->A) + { + XCPU_SET_STATUS_BIT (XCPU_JUMP2); + XCPU_SET_STATUS_BIT (XCPU_JUMP1); + } +} + +void +in_halt (uint8_t ia) +{ + cpu->halted = 1; +} + +void +in_nop (uint8_t ia) +{ + /* do nothing */ +} + + +uint8_t +xcpu_get_instruction (void) +{ + uint32_t aip; /* actual IP */ + uint8_t is; /* instruction */ + + /* find out where IP is pointing */ + if (cpu->IP >= cpu->rom_start && cpu->IP < cpu->rom_start + cpu->rom_size) + { + /* IP is in ROM */ + aip = cpu->IP - cpu->rom_start; + is = rom[aip]; + dis = disassemble_instruction (rom + aip); + } + else if (cpu->IP >= cpu->memory_start + && cpu->IP < cpu->memory_start + cpu->memory_size) + { + /* IP is in main memory */ + aip = cpu->IP - cpu->memory_start; + is = memory[aip]; + dis = disassemble_instruction (memory + aip); + } + else + { + /* IP is in lala land - throw exception and return halt instruction */ + exception ("IP points outside of memory and ROM"); + is = 0xe0; + dis = NULL; + } + + cpu->IP++; + + return is; +} + +void +xcpu_dispatch_instruction (uint8_t instruction) +{ + uint8_t it; /* instruction type */ + uint8_t ia; /* instruction args */ + + /* split the instruction and the arg */ + it = (instruction & 0xf0) >> 4; + ia = instruction & 0x0f; + + /* call the instruction */ + instructions[it](ia); +} + +void +xcpu_print_cpu_state (void) +{ + printf ("Current CPU state\n" + "IP %08x SP %08x M %08x Status %02x\n" + "A %08x B %08x C %08x D %08x\n" + "\n" + "Memory state\n" + "Rom %08x - %08x\n" + "IO Ports %08x - %08x\n" + "Memory %08x - %08x\n" + "Stack %08x - %08x\n" + "\n", + cpu->IP, cpu->SP, cpu->M, cpu->status, + cpu->A, cpu->B, cpu->C, cpu->D, + cpu->rom_start, cpu->rom_start + cpu->rom_size, + cpu->ioport_start, cpu->ioport_start + 12, + cpu->memory_start, cpu->memory_start + cpu->memory_size, + cpu->stack_start, cpu->stack_start + cpu->stack_size); + + if (cpu->stack_start > 0) + { + uint32_t sa; + + sa = cpu->SP - cpu->memory_start; + + if (cpu->SP > cpu->stack_start) + printf ("Stack-4: %02x%02x%02x%02x\n", + memory[sa-4], memory[sa-3], memory[sa-2], memory[sa-1]); + else + printf ("Stack-4: 00000000\n"); + if (cpu->SP > cpu->stack_start + 4) + printf ("Stack-8: %02x%02x%02x%02x\n", + memory[sa-8], memory[sa-7], memory[sa-6], memory[sa-5]); + else + printf ("Stack-8: 00000000\n"); + if (cpu->SP > cpu->stack_start + 8) + printf ("Stack-c: %02x%02x%02x%02x\n", + memory[sa-12], memory[sa-11], memory[sa-10], memory[sa-9]); + else + printf ("Stack-c: 00000000\n"); + if (cpu->SP > cpu->stack_start + 12) + printf ("Stack-10: %02x%02x%02x%02x\n", + memory[sa-16], memory[sa-15], memory[sa-14], memory[sa-13]); + else + printf ("Stack-10: 00000000\n"); + } +} + +int +xcpu_halted (void) +{ + return cpu->halted; +} + + +void +xcpu_console_seek (uint32_t data) +{ + /* console device doesn't seek (maybe it will in the future) */ +} + +uint32_t +xcpu_console_tell (void) +{ + /* doesn't tell either */ + return 0; +} + +uint32_t +xcpu_console_read (void) +{ + uint32_t data; + + data = (uint32_t)getchar (); + + return bswap4 (data); +} + +void +xcpu_console_write (uint32_t data) +{ + putchar ((int)data); +} + +void +xcpu_graphics_seek (uint32_t data) +{ +} + +uint32_t +xcpu_graphics_tell (void) +{ + return 0; +} + +uint32_t +xcpu_graphics_read (void) +{ + return 0; +} + +void +xcpu_graphics_write (uint32_t data) +{ +} + +void +xcpu_exe_seek (uint32_t pos) +{ + fseek (infp, (long)pos, SEEK_SET); +} + +uint32_t +xcpu_exe_tell (void) +{ + uint32_t p; + + p = (uint32_t)ftell (infp); + + return p; +} + +uint32_t +xcpu_exe_read (void) +{ + uint32_t data; + + fread (&data, sizeof (uint32_t), 1, infp); + + return bswap4 (data); +} + +void +xcpu_exe_write (uint32_t data) +{ + /* EXE is a read only device */ +} + +/* xcpu_init + * Initialize XCPU to a known state and load the bootstrap "ROM" into memory. + */ +void +xcpu_init (void) +{ + long offset; + FILE *bs; + + /* assign instructions to their slots for dispatching */ + + instructions[0] = in_nop; + instructions[1] = in_jump; + instructions[2] = in_push; + instructions[3] = in_pop; + instructions[4] = in_memop; + instructions[5] = in_cpuop; + instructions[6] = in_assign; + instructions[7] = in_add; + instructions[8] = in_shift; + instructions[9] = in_and; + instructions[10] = in_or; + instructions[11] = in_not; + instructions[12] = in_move; + instructions[13] = in_compare; + instructions[14] = in_halt; + instructions[15] = in_config; + + + /* allocate the CPU and ROM */ + + cpu = xmalloc (XCPU_SIZE); + rom = xmalloc (ROM_SIZE); + + + /* load the bootstrap into ROM */ + + bs = fopen (BOOTSTRAP_FILE, "rb"); + if (bs == NULL) + { + fprintf (stderr, "XCPU: Unable to open bootstrap file `%s'\n", + BOOTSTRAP_FILE); + exit (1); + } + + fseek (bs, 4, SEEK_SET); + fread (&offset, 4, 1, bs); + offset = (long)bswap4 ((uint32_t)offset); + fseek (bs, offset, SEEK_SET); + + fread (rom, sizeof (uint8_t), ROM_SIZE, bs); + + fclose (bs); + + + /* initialize devices */ + + /* console I/O */ + ioports[0] = xmalloc (sizeof (_ioport)); + ioports[0]->seek = xcpu_console_seek; + ioports[0]->tell = xcpu_console_tell; + ioports[0]->read = xcpu_console_read; + ioports[0]->write = xcpu_console_write; + + /* graphics I/O */ + ioports[1] = xmalloc (sizeof (_ioport)); + ioports[1]->seek = xcpu_graphics_seek; + ioports[1]->tell = xcpu_graphics_tell; + ioports[1]->read = xcpu_graphics_read; + ioports[1]->write = xcpu_graphics_write; + + /* exe I/O - this is the program to be executed */ + ioports[7] = xmalloc (sizeof (_ioport)); + ioports[7]->seek = xcpu_exe_seek; + ioports[7]->tell = xcpu_exe_tell; + ioports[7]->read = xcpu_exe_read; + ioports[7]->write = xcpu_exe_write; + + + /* initialize CPU state */ + + cpu->IP = 0; + cpu->rom_start = 0; + cpu->rom_size = ROM_SIZE; + cpu->halted = 0; +} + + +/* xcpu_cleanup + * Clean up the operating environment after execution finishes. + */ +void +xcpu_cleanup (void) +{ + int k; + + for (k = 0; k < N_IOPORTS; k++) + if (ioports[k] != NULL) + xfree (ioports[k]); + + if (memory != NULL) + xfree (memory); + + xfree (rom); + xfree (cpu); + + free_all (); +} + +/* xcpu_load_file + * Load the file to be executed and place the file pointer into the + * device structure so that the bootstrap code can access it. + */ +void +xcpu_load_file (char *filename) +{ + char header[5]; + + infp = fopen (filename, "rb"); + if (infp == NULL) + { + fprintf (stderr, "XCPU: Unable to open input file `%s'\n", filename); + exit (1); + } + + /* verify that this is a good file */ + fread (header, sizeof (char), 4, infp); + header[4] = '\0'; + + if (strcmp (header, "XCPU")) + { + fprintf (stderr, "XCPU: `%s' is not a correct executable image\n", + filename); + fclose (infp); + exit (1); + } + + fseek (infp, 0, SEEK_SET); +} + + +int +main (int argc, char *argv[]) +{ + if (argc != 2) + { + fprintf (stderr, "XCPU: must specify input file\n"); + exit (1); + } + + xcpu_init (); + xcpu_load_file (argv[1]); + + do { + uint8_t instruction; + + instruction = xcpu_get_instruction (); + xcpu_dispatch_instruction (instruction); + + icount++; + + if (XCPU_GET_STATUS_BIT (XCPU_DEBUG)) + { + printf ("%d: %02x: %s\n", icount, instruction, dis); + xcpu_print_cpu_state (); + getchar (); + } + + xfree (dis); + + } while (!xcpu_halted ()); + + xcpu_cleanup (); + + return 0; +} diff --git a/xcpu/xcpu_asm.h b/xcpu/xcpu_asm.h new file mode 100644 index 0000000..39b2c7c --- /dev/null +++ b/xcpu/xcpu_asm.h @@ -0,0 +1,50 @@ + ;; XCPU_ASM.h -*- asm-mode -*- + ;; Standard XCPU definitions + + + ;; CPU flags for cpuop + _define CPU_JUMP1 0 + _define CPU_JUMP2 1 + _define CPU_MEMOP 2 + _define CPU_SLR 3 + _define CPU_SRC 4 + _define CPU_MOVE 5 + _define CPU_DEBUG 7 + + _define CPU_MEMOP_READ 1 + _define CPU_MEMOP_WRITE 0 + + _define CPU_SHIFT_LEFT 0 + _define CPU_SHIFT_RIGHT 1 + _define CPU_SHIFT_REG 0 + _define CPU_SHIFT_CIRC 1 + + _define CPU_MOVE_TO_M 0 + _define CPU_MOVE_FROM_M 1 + + ;; jump types + _define JUMP_UNCOND 0 + _define JUMP_EQ 1 + _define JUMP_NEQ 2 + _define JUMP_LT 3 + _define JUMP_GT 4 + ;;_define JUMP_LE 5 + ;;_define JUMP_GE 6 + _define CALL 7 + + ;; devices to configure for config + _define CONFIG_MEMORY 0 + _define CONFIG_STACK 1 + _define CONFIG_IOPORTS 2 + + ;; I/O devices + _define IO_DEV_CONSOLE 0x0 + _define IO_DEV_GRAPHICS 0x1 + + ;; IO addresses and controls + _define IOPORT_DEVICE 0x0104 + _define IOPORT_ADDR 0x0108 + _define IOPORT_RW 0x010c + + ;; address of pointer to halt routine + _define HALT_ADDR 0xfc diff --git a/xcpu/xdis.c b/xcpu/xdis.c new file mode 100644 index 0000000..22882bb --- /dev/null +++ b/xcpu/xdis.c @@ -0,0 +1,116 @@ +/* xdis.c + * Disassembler for XCPU. + * + */ + +#include <stdio.h> +#include <getopt.h> + +#define PROGRAM_NAME "XAS" +#define VERSION "0.9" +#define AUTHOR "Jonathan duSaint" +#define AUTHOR_EMAIL "jon@rockgeeks.net" + +char pname[]; +char version[]; +char author[]; +char author_email[]; + + +void +print_version (void) +{ + printf ("%s %s\n" + "Copyright (C) 2006 %s <%s>\n" + "%s is free software under the terms of the GNU General Public\n" + "License version 2; see the file COPYING in the source\n" + "distribution for details. This software comes with ABSOLUTELY\n" + "NO WARRANTY.\n", + pname, version, author, author_email, pname); +} + +void +help (void) +{ + printf ( +"Usage: xas [options] file\n" +"Assembler for XCPU\n" +"\n" +"Options:\n" +" -h, --help display this help and exit\n" +" -v, --version output version information and exit\n" +" -V, --verbose print status information while running\n" +" -L, --LOUD print even more information - probably only useful\n" +" for debugging XAS\n" +" -a<address>, --address=<address> specify load address, overriding\n" +" default and any specified in file\n" +" -D<macro>=<val> define a <macro> with <val> as its value\n" +" -g, --debug include debug information in the object file\n" +" -o <file>, --output=<file> specify name of output file\n"); +} + + +int +main (int argc, char *argv[]) +{ + struct option options[] = { + { "output", required_argument, NULL, 'o' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'v' }, + { 0, 0, 0, 0 }, + }; + + opterr = 0; + + while (1) + { + opt = getopt_long (argc, argv, "o:hv", options, &index); + + if (opt == EOF) break; + + switch (opt) + { + case 'o': + output_file = xmalloc (strlen (optarg) + 1); + strcpy (output_file, optarg); + break; + case 'h': + help (); + return 0; + case 'v': + print_version (); + return 0; + case ':': + fprintf (stderr, "%s: GETOPT internal error\n", pname); + exit (1); + case '?': + fprintf (stderr, "%s: unknown option character `%c'\n", + pname, optopt); + break; + default: + fprintf (stderr, "%s: ?? getopt returned character code 0x%x ??\n", + pname, opt); + } + } + + input_file = argv[optind]; + + if (output_file == NULL) + { + size_t nchar = strlen (input_file); + + output_file = xmalloc (nchar + 3); + if (input_file[nchar-2] == '.' && input_file[nchar-1] == 's') nchar -= 2; + + strncpy (output_file, input_file, nchar); + strcat (output_file, ".s"); + } + + + + + xfree (output_file); + free_all (); + + return 0; +} diff --git a/xcpu/xlib.s b/xcpu/xlib.s new file mode 100644 index 0000000..0cae2c2 --- /dev/null +++ b/xcpu/xlib.s @@ -0,0 +1,14 @@ + ;; xlib.s - Miscellaneous library routines to make XAS a little easier + + + ;; sub - C = B - A + _label sub + + + + ;; mul: C = B * A + _label mul + + + ;; div: C = B / A + _label div |
