A trivial line macro processing program and a defition files for converting assembly names to their matching hex while preserving information required by a stage1 assembler
This commit is contained in:
parent
424dfa16b8
commit
2b537ff78a
|
@ -0,0 +1,251 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#define max_string 63
|
||||
|
||||
FILE* source_file;
|
||||
bool Reached_EOF;
|
||||
uint32_t ip;
|
||||
|
||||
struct Token
|
||||
{
|
||||
struct Token* next;
|
||||
uint8_t type;
|
||||
char* Text;
|
||||
char* Expression;
|
||||
};
|
||||
|
||||
enum type
|
||||
{
|
||||
macro = 1,
|
||||
};
|
||||
|
||||
struct Token* newToken()
|
||||
{
|
||||
struct Token* p;
|
||||
|
||||
p = calloc (1, sizeof (struct Token));
|
||||
if (NULL == p)
|
||||
{
|
||||
fprintf (stderr, "calloc failed.\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
struct Token* addToken(struct Token* head, struct Token* p)
|
||||
{
|
||||
if(NULL == head)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
if(NULL == head->next)
|
||||
{
|
||||
head->next = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
addToken(head->next, p);
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
void purge_lineComment()
|
||||
{
|
||||
int c = fgetc(source_file);
|
||||
while((10 != c) && (13 != c))
|
||||
{
|
||||
c = fgetc(source_file);
|
||||
}
|
||||
}
|
||||
|
||||
struct Token* Tokenize_Line(struct Token* head)
|
||||
{
|
||||
char* store = calloc(max_string + 1, sizeof(char));
|
||||
int32_t c;
|
||||
uint32_t i;
|
||||
struct Token* p = newToken();
|
||||
|
||||
Restart:
|
||||
for(i = 0; i < max_string; i = i + 1)
|
||||
{
|
||||
c = fgetc(source_file);
|
||||
if(-1 == c)
|
||||
{
|
||||
Reached_EOF = true;
|
||||
goto Token_complete;
|
||||
}
|
||||
else if((10 == c) || (13 == c))
|
||||
{
|
||||
if(1 > i)
|
||||
{
|
||||
goto Restart;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto Token_complete;
|
||||
}
|
||||
}
|
||||
else if((35 == c) || (59 == c))
|
||||
{
|
||||
purge_lineComment();
|
||||
goto Restart;
|
||||
}
|
||||
else if((32 == c) || (9 == c))
|
||||
{
|
||||
if(1 > i)
|
||||
{
|
||||
goto Restart;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto Token_complete;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
store[i] = (char)c;
|
||||
}
|
||||
}
|
||||
|
||||
Token_complete:
|
||||
p->Text = store;
|
||||
return addToken(head, p);
|
||||
}
|
||||
|
||||
void setExpression(struct Token* p, char match[], char Exp[])
|
||||
{
|
||||
if(NULL != p->next)
|
||||
{
|
||||
setExpression(p->next, match, Exp);
|
||||
}
|
||||
|
||||
/* Leave macros alone */
|
||||
if((p->type & macro))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* Only if there is an exact match replace */
|
||||
if(0 == strncmp(p->Text, match, max_string))
|
||||
{
|
||||
p->Expression = Exp;
|
||||
}
|
||||
}
|
||||
|
||||
void identify_macros(struct Token* p)
|
||||
{
|
||||
if(0 == strncmp(p->Text, "DEFINE", max_string))
|
||||
{
|
||||
p->type = macro;
|
||||
p->next->type = macro;
|
||||
p->next->next->type = macro;
|
||||
}
|
||||
|
||||
if(NULL != p->next)
|
||||
{
|
||||
identify_macros(p->next);
|
||||
}
|
||||
}
|
||||
|
||||
void line_macro(struct Token* p)
|
||||
{
|
||||
if(0 == strncmp(p->Text, "DEFINE", max_string))
|
||||
{
|
||||
setExpression(p, p->next->Text, p->next->next->Text);
|
||||
}
|
||||
|
||||
if(NULL != p->next)
|
||||
{
|
||||
line_macro(p->next);
|
||||
}
|
||||
}
|
||||
|
||||
void preserve_other(struct Token* p)
|
||||
{
|
||||
if(NULL != p->next)
|
||||
{
|
||||
preserve_other(p->next);
|
||||
}
|
||||
|
||||
if((NULL == p->Expression) && !(p->type & macro))
|
||||
{
|
||||
p->Expression = p->Text;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t numerate_string(char a[])
|
||||
{
|
||||
char *ptr;
|
||||
return (uint16_t)strtol(a, &ptr, 0);
|
||||
}
|
||||
|
||||
void eval_immediates(struct Token* p)
|
||||
{
|
||||
if(NULL != p->next)
|
||||
{
|
||||
eval_immediates(p->next);
|
||||
}
|
||||
|
||||
if((NULL == p->Expression) && !(p->type & macro))
|
||||
{
|
||||
uint16_t value;
|
||||
value = numerate_string(p->Text);
|
||||
|
||||
if(('0' == p->Text[0]) || (0 != value))
|
||||
{
|
||||
char* c = calloc(5, sizeof(char));
|
||||
sprintf(c, "%04x", value);
|
||||
p->Expression = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_hex(struct Token* p)
|
||||
{
|
||||
if(NULL != p->Expression)
|
||||
{
|
||||
fprintf(stdout, "\n%s", p->Expression);
|
||||
}
|
||||
|
||||
if(NULL != p->next)
|
||||
{
|
||||
print_hex(p->next);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* Standard C main program */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* Make sure we have a program tape to run */
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s $FileName\nWhere $FileName is the name of the paper tape of the program being run\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
source_file = fopen(argv[1], "r");
|
||||
|
||||
Reached_EOF = false;
|
||||
ip = 0;
|
||||
struct Token* head = NULL;
|
||||
while(!Reached_EOF)
|
||||
{
|
||||
head = Tokenize_Line(head);
|
||||
}
|
||||
|
||||
identify_macros(head);
|
||||
line_macro(head);
|
||||
eval_immediates(head);
|
||||
preserve_other(head);
|
||||
print_hex(head);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
|
@ -0,0 +1,217 @@
|
|||
#Registers
|
||||
DEFINE R0 0
|
||||
DEFINE R1 1
|
||||
DEFINE R2 2
|
||||
DEFINE R3 3
|
||||
DEFINE R4 4
|
||||
DEFINE R5 5
|
||||
DEFINE R6 6
|
||||
DEFINE R7 7
|
||||
DEFINE R8 8
|
||||
DEFINE R9 9
|
||||
DEFINE R10 A
|
||||
DEFINE R11 B
|
||||
DEFINE R12 C
|
||||
DEFINE R13 D
|
||||
DEFINE R14 E
|
||||
DEFINE R15 F
|
||||
|
||||
# 4OP Integer Group
|
||||
DEFINE ADD.CI 0100
|
||||
DEFINE ADD.CO 0101
|
||||
DEFINE ADD.CIO 0102
|
||||
DEFINE ADDU.CI 0103
|
||||
DEFINE ADDU.CO 0104
|
||||
DEFINE ADDU.CIO 0105
|
||||
DEFINE SUB.BI 0106
|
||||
DEFINE SUB.BO 0107
|
||||
DEFINE SUB.BIO 0108
|
||||
DEFINE SUBU.BI 0109
|
||||
DEFINE SUBU.BO 010A
|
||||
DEFINE SUBU.BIO 010B
|
||||
DEFINE MULTIPLY 010C
|
||||
DEFINE MULTIPLYU 010D
|
||||
DEFINE DIVIDE 010E
|
||||
DEFINE DIVIDEU 010F
|
||||
DEFINE MUX 0110
|
||||
DEFINE NMUX 0111
|
||||
DEFINE SORT 0112
|
||||
DEFINE SORTU 0113
|
||||
|
||||
# 3OP Integer Group
|
||||
DEFINE ADD 05000
|
||||
DEFINE ADDU 05001
|
||||
DEFINE SUB 05002
|
||||
DEFINE SUBU 05003
|
||||
DEFINE CMP 05004
|
||||
DEFINE CMPU 05005
|
||||
DEFINE MUL 05006
|
||||
DEFINE MULH 05007
|
||||
DEFINE MULU 05008
|
||||
DEFINE MULUH 05009
|
||||
DEFINE DIV 0500A
|
||||
DEFINE MOD 0500B
|
||||
DEFINE DIVU 0500C
|
||||
DEFINE MODU 0500D
|
||||
DEFINE MAX 05010
|
||||
DEFINE MAXU 05011
|
||||
DEFINE MIN 05012
|
||||
DEFINE MINU 05013
|
||||
DEFINE PACK 05014
|
||||
DEFINE UNPACK 05015
|
||||
DEFINE PACK8.CO 05016
|
||||
DEFINE PACK8U.CO 05017
|
||||
DEFINE PACK16.CO 05018
|
||||
DEFINE PACK16U.CO 05019
|
||||
DEFINE PACK32.CO 0501A
|
||||
DEFINE PACK32U.CO 0501B
|
||||
DEFINE AND 05020
|
||||
DEFINE OR 05021
|
||||
DEFINE XOR 05022
|
||||
DEFINE NAND 05023
|
||||
DEFINE NOR 05024
|
||||
DEFINE XNOR 05025
|
||||
DEFINE MPQ 05026
|
||||
DEFINE LPQ 05027
|
||||
DEFINE CPQ 05028
|
||||
DEFINE BPQ 05029
|
||||
DEFINE SAL 05030
|
||||
DEFINE SAR 05031
|
||||
DEFINE SL0 05032
|
||||
DEFINE SR0 05033
|
||||
DEFINE SL1 05034
|
||||
DEFINE SR1 05035
|
||||
DEFINE ROL 05036
|
||||
DEFINE ROR 05037
|
||||
DEFINE LOADX 05038
|
||||
DEFINE LOADX8 05039
|
||||
DEFINE LOADXU8 0503A
|
||||
DEFINE LOADX16 0503B
|
||||
DEFINE LOADXU16 0503C
|
||||
DEFINE LOADX32 0503D
|
||||
DEFINE LOADXU32 0503E
|
||||
DEFINE STOREX 05048
|
||||
DEFINE STOREX8 05049
|
||||
DEFINE STOREX16 0504A
|
||||
DEFINE STOREX32 0504B
|
||||
|
||||
# 2OP Integer Group
|
||||
DEFINE NEG 090000
|
||||
DEFINE ABS 090001
|
||||
DEFINE NABS 090002
|
||||
DEFINE SWAP 090003
|
||||
DEFINE COPY 090004
|
||||
DEFINE MOVE 090005
|
||||
DEFINE BRANCH 090100
|
||||
DEFINE CALL 090101
|
||||
DEFINE PUSHR 090200
|
||||
DEFINE PUSH8 090201
|
||||
DEFINE PUSH16 090202
|
||||
DEFINE PUSH32 090203
|
||||
DEFINE POPR 090280
|
||||
DEFINE POP8 090281
|
||||
DEFINE POPU8 090282
|
||||
DEFINE POP16 090283
|
||||
DEFINE POPU16 090284
|
||||
DEFINE POP32 090285
|
||||
DEFINE POPU32 090286
|
||||
|
||||
# 1OP Group
|
||||
DEFINE READPC 0D00000
|
||||
DEFINE READSCID 0D00001
|
||||
DEFINE FALSE 0D00002
|
||||
DEFINE TRUE 0D00003
|
||||
DEFINE JSR_COROUTINE 0D01000
|
||||
DEFINE RET 0D01001
|
||||
DEFINE PUSHPC 0D02000
|
||||
DEFINE POPPC 0D02001
|
||||
|
||||
# 2OPI Group
|
||||
DEFINE ADDI 0E
|
||||
DEFINE ADDUI 0F
|
||||
DEFINE SUBI 10
|
||||
DEFINE SUBUI 11
|
||||
DEFINE CMPI 12
|
||||
DEFINE LOAD 13
|
||||
DEFINE LOAD8 14
|
||||
DEFINE LOADU8 15
|
||||
DEFINE LOAD16 16
|
||||
DEFINE LOADU16 17
|
||||
DEFINE LOAD32 18
|
||||
DEFINE LOADU32 19
|
||||
DEFINE CMPUI 1F
|
||||
DEFINE STORE 20
|
||||
DEFINE STORE8 21
|
||||
DEFINE STORE16 22
|
||||
DEFINE STORE32 23
|
||||
DEFINE CMPJUMP.G C0
|
||||
DEFINE CMPJUMP.GE C1
|
||||
DEFINE CMPJUMP.E C2
|
||||
DEFINE CMPJUMP.NE C3
|
||||
DEFINE CMPJUMP.LE C4
|
||||
DEFINE CMPJUMP.L C5
|
||||
DEFINE CMPJUMPU.G D0
|
||||
DEFINE CMPJUMPU.GE D1
|
||||
DEFINE CMPJUMPU.LE D4
|
||||
DEFINE CMPJUMPU.L D5
|
||||
|
||||
# 1OPI Group
|
||||
DEFINE JUMP.C 2C0
|
||||
DEFINE JUMP.B 2C1
|
||||
DEFINE JUMP.O 2C2
|
||||
DEFINE JUMP.G 2C3
|
||||
DEFINE JUMP.GE 2C4
|
||||
DEFINE JUMP.E 2C5
|
||||
DEFINE JUMP.NE 2C6
|
||||
DEFINE JUMP.LE 2C7
|
||||
DEFINE JUMP.L 2C8
|
||||
DEFINE JUMP.Z 2C9
|
||||
DEFINE JUMP.NZ 2CA
|
||||
DEFINE JUMP.P 2CB
|
||||
DEFINE JUMP.NP 2CC
|
||||
DEFINE CALLI 2D0
|
||||
DEFINE LOADI 2D1
|
||||
DEFINE LOADUI 2D2
|
||||
DEFINE SALI 2D3
|
||||
DEFINE SARI 2D4
|
||||
DEFINE SL0I 2D5
|
||||
DEFINE SR0I 2D6
|
||||
DEFINE SL1I 2D7
|
||||
DEFINE SR1I 2D8
|
||||
DEFINE LOADR 2E0
|
||||
DEFINE LOADR8 2E1
|
||||
DEFINE LOADRU8 2E2
|
||||
DEFINE LOADR16 2E3
|
||||
DEFINE LOADRU16 2E4
|
||||
DEFINE LOADR32 2E5
|
||||
DEFINE LOADRU32 2E6
|
||||
DEFINE STORER 2F0
|
||||
DEFINE STORER8 2F1
|
||||
DEFINE STORER16 2F2
|
||||
DEFINE STORER32 2F3
|
||||
DEFINE CMPSKIP.G A00
|
||||
DEFINE CMPSKIP.GE A01
|
||||
DEFINE CMPSKIP.E A02
|
||||
DEFINE CMPSKIP.NE A03
|
||||
DEFINE CMPSKIP.LE A04
|
||||
DEFINE CMPSKIP.L A05
|
||||
DEFINE CMPSKIPU.G A10
|
||||
DEFINE CMPSKIPU.GE A11
|
||||
DEFINE CMPSKIPU.LE A14
|
||||
DEFINE CMPSKIPU.L A15
|
||||
|
||||
# 0OPI Group
|
||||
DEFINE JUMP 3C00
|
||||
|
||||
# HALCODE Group
|
||||
DEFINE FOPEN_READ 42100000
|
||||
DEFINE FOPEN_WRITE 42100001
|
||||
DEFINE FCLOSE 42100002
|
||||
DEFINE REWIND 42100003
|
||||
DEFINE FSEEK 42100004
|
||||
DEFINE FGETC 42100100
|
||||
DEFINE FPUTC 42100200
|
||||
|
||||
# 0OP Group
|
||||
DEFINE NOP 00000000
|
||||
DEFINE HALT FFFFFFFF
|
Loading…
Reference in New Issue