ASM: Added support for defining arbitrary double-words

This commit is contained in:
(Tim) Efthimis Kritikos 2024-02-14 12:57:07 +00:00
parent d8c50156a8
commit d643dc3e55
4 changed files with 68 additions and 49 deletions

View File

@ -230,13 +230,13 @@ char *disassemble(uint32_t opcode_be){
return ret; return ret;
} }
int32_t parse_immediate(char* str,struct assembler_context_t *assembler_context){ int64_t parse_immediate(char* str,struct assembler_context_t *assembler_context){
if(*str=='0'){ if(*str=='0'){
uint32_t addr; uint32_t addr;
if(sscanf(str,"%06X",&addr)!=1) if(sscanf(str,"0x%08X",&addr)!=1){
return -1; return -1;
else }else
return addr&0x00FFFFFF; return addr;
}else{ }else{
// Get name size // Get name size
int len=0; int len=0;
@ -259,7 +259,7 @@ int32_t parse_immediate(char* str,struct assembler_context_t *assembler_context)
} }
} }
uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context){ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context){
uint16_t opcode; uint16_t opcode;
int x; int x;
int call; int call;
@ -290,26 +290,26 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context
opcode|=0x09; opcode|=0x09;
x++; x++;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
while(line[x]==' ')x++; while(line[x]==' ')x++;
if(line[x]=='$'){ if(line[x]=='$'){
x++; x++;
int32_t imm=parse_immediate(line+x,assembler_context); int64_t imm=parse_immediate(line+x,assembler_context);
if(imm<0){ if(imm<0){
switch(imm){ switch(imm){
default: default:
return 0xFFFFFFFF; return -2;
case -2: case -2:
return 0xFFFFFFFE; return -3;
} }
}else }else
return opcode<<24|(imm&0x00FFFFFF); return opcode<<24|(imm&0x00FFFFFF);
}else }else
return 0xFFFFFFFF; return -2;
}else{ }else{
uint8_t r1,r0,params; uint8_t r1,r0,params;
if(strncmp(line,"ADD ",4)==0){ if(strncmp(line,"ADD ",4)==0){
@ -356,21 +356,21 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context
r1=line[x]-'0'; r1=line[x]-'0';
return (opcode<<16)|r0<<8|r1; return (opcode<<16)|r0<<8|r1;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return (opcode<<16)|r0<<8; return (opcode<<16)|r0<<2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else if(strncmp(line,"MOV ",4)==0){ }else if(strncmp(line,"MOV ",4)==0){
uint32_t data; uint32_t data;
x=3; x=3;
@ -399,23 +399,23 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context
else if(line[x]=='h') else if(line[x]=='h')
return 0x41000000|r1<<16|(data&0xFFFF); return 0x41000000|r1<<16|(data&0xFFFF);
else else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else if(line[x]=='S'){ }else if(line[x]=='S'){
x++; x++;
if(line[x]=='P'){ if(line[x]=='P'){
return 0x0F000000|(data&0x00FFFFFF); return 0x0F000000|(data&0x00FFFFFF);
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else if(line[x]=='%'){ }else if(line[x]=='%'){
x++; x++;
if(line[x]=='R'){ if(line[x]=='R'){
@ -441,34 +441,34 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context
if(line[x]==')'){ if(line[x]==')'){
return 0x20080000|(r0&0xFF)<<8|(r1&0xFF); return 0x20080000|(r0&0xFF)<<8|(r1&0xFF);
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else }else
return 0xFFFFFFFF; return -2;
}else if(strncmp(line,"HALT ",5)==0||strncmp(line,"HALT\0",5)==0){ }else if(strncmp(line,"HALT ",5)==0||strncmp(line,"HALT\0",5)==0){
x=4; x=4;
while(line[x]==' ')x++; while(line[x]==' ')x++;
if(line[x]==0) if(line[x]==0)
return 0x20050000; return 0x20050000;
else else
return 0xFFFFFFFF; return -2;
}else if(strncmp(line,"RET ",4)==0||strncmp(line,"RET\0",4)==0){ }else if(strncmp(line,"RET ",4)==0||strncmp(line,"RET\0",4)==0){
return 0x10000000; return 0x10000000;
}else if(*line==':'){ }else if(*line==':'){
return 0xFFFFFFF0; return -1;
}else if(strncmp(line,"PUSH ",5)==0){ }else if(strncmp(line,"PUSH ",5)==0){
x=4; x=4;
while(line[x]==' ')x++; while(line[x]==' ')x++;
@ -479,10 +479,10 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context
if(line[x]>='0'&&line[x]<='7') if(line[x]>='0'&&line[x]<='7')
return 0x20060000|(line[x]-'0')<<8; return 0x20060000|(line[x]-'0')<<8;
else else
return 0xFFFFFFFF; return -2;
} }
}else }else
return 0xFFFFFFFF; return -2;
}else if(strncmp(line,"POP ",4)==0){ }else if(strncmp(line,"POP ",4)==0){
x=3; x=3;
while(line[x]==' ')x++; while(line[x]==' ')x++;
@ -493,12 +493,29 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context
if(line[x]>='0'&&line[x]<='7') if(line[x]>='0'&&line[x]<='7')
return 0x20070000|(line[x]-'0')<<8; return 0x20070000|(line[x]-'0')<<8;
else else
return 0xFFFFFFFF; return -2 ;
} }
}else }else
return 0xFFFFFFFF; return -2;
}else if(strncmp(line,"DDW ",4)==0){
x=4;
while(line[x]==' ')x++;
if(line[x]=='$'){
x++;
int64_t imm=parse_immediate(line+x,assembler_context);
if(imm<0){
switch(imm){
default:
return -2;
case -2:
return -3;
} }
return 0xFFFFFFFF; }else
return imm;
}else
return -2;
}
return -2;//TODO: figure out which statemnt falls through to this
} }
} }

View File

@ -13,7 +13,7 @@ struct assembler_context_t{
uint32_t current_location; uint32_t current_location;
}; };
uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context); int64_t assemble_line(char *line, struct assembler_context_t *assembler_context);
int assembler_context_process(char* line,struct assembler_context_t *tofree); int assembler_context_process(char* line,struct assembler_context_t *tofree);
struct assembler_context_t *malloc_assembler_context(); struct assembler_context_t *malloc_assembler_context();

10
main.c
View File

@ -92,7 +92,7 @@ int main(int argc, char* argd[] ){
int linec=0; int linec=0;
if (rom != NULL) { if (rom != NULL) {
char line [1000]; char line [1000];
uint32_t opcode; int64_t opcode;
uint32_t addr=0; uint32_t addr=0;
//--------------- Build assembler context -------------------- //--------------- Build assembler context --------------------
@ -126,16 +126,16 @@ int main(int argc, char* argd[] ){
opcode=assemble_line(line,assembler_context); opcode=assemble_line(line,assembler_context);
if(opcode>=0xFFFFFFF1){ if(opcode<-1){
if(opcode==0xFFFFFFFE) if(opcode==-3)
printf("Error label not found %s:%d\n", infile,linec+1); printf("Error label not found %s:%d\n", infile,linec+1);
else else
printf("Error assembling %s:%d\n", infile,linec+1); printf("Error assembling (0x%lx)%s:%d\n",(uint64_t)opcode, infile,linec+1);
fclose(rom); fclose(rom);
free_assembler_context(assembler_context); free_assembler_context(assembler_context);
free_simdata(simdata); free_simdata(simdata);
return 1; return 1;
}else if(opcode==0xFFFFFFF0){ }else if(opcode==-1){
;/*it was empty, comment or label*/ ;/*it was empty, comment or label*/
}else{ }else{
simdata->RAM[addr]=(0xFF000000&opcode)>>24; simdata->RAM[addr]=(0xFF000000&opcode)>>24;

View File

@ -49,3 +49,5 @@ MOV $0xcccd,%R1l
MOV $0x3f4c,%R1h MOV $0x3f4c,%R1h
MOV %R1,(%R0) MOV %R1,(%R0)
HALT HALT
:TR_LOW
DDW $0xDEADBEEF