OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

Compare Revisions

  • This comparison shows the changes necessary to convert path
    /zipcpu/trunk/sw/zasm
    from Rev 26 to Rev 34
    Reverse comparison

Rev 26 → Rev 34

/test.S
83,7 → 83,7
#define SHIFT_TEST
#define TRAP_TEST
#define MPY_TEST
// #define PUSH_TEST
#define PUSH_TEST
test:
#ifdef DO_TEST_ASSEMBLER
; We start out by testing our assembler. We give it some instructions, which
164,7 → 164,17
lod dead_beef.values(r10),r10 ; r10 now equals 0xdeadbeef
cmp r10,r9
bnz test_failure
 
; Test whether or not our operator precedence rules work
ldi 5+3*8,r0
ldi 3*8+5,r1
cmp r0,r1
bnz test_failure
ldi (5+3)*8,r0
ldi 8*(3+5),r1
cmp r0,r1
bnz test_failure
 
; Test whether or not we can properly decode OCTAL values
clr r0 ; Re-clear our register set first
clr r1
267,6 → 277,7
ldi $c0000000h,r12 ; Set R12 to point to our peripheral address
mov r12,ur12
mov test_start(pc),upc
mov stack(pc),usp
ldi 0x8000ffff,r0 ; Clear interrupts, turn all vectors off
sto r0,(r12)
rtu
493,7 → 504,18
cmp r0,r1
trap.ne r11
#endif
 
#ifdef PUSH_TEST
ldi $0x0e000,r11 // Mark our test
ldi 0x01248cab,r0
ldi 0xd5312480,r1 // Let's see if we can preserve this as well
mov r1,r7
JSR(reverse_bit_order,R4); // *SP = 0x010013d
cmp r0,r1
trap.ne r11
cmp r0,r7
trap.ne r11
#endif
// Return success / Test the trap interrupt
clr r11
trap r11
512,6 → 534,7
PUSH(R2,SP)
LDI 32,R1
CLR R2
reverse_bit_order_loop:
LSL 1,R2
LSR 1,R0
OR.C 1,R2
523,5 → 546,5
RET
#endif
fill 512,0
stack:
stack: // Must point to a valid word initially
word 0
/zasm.y
82,6 → 82,7
%left BOOLEANAND
%left BITWISEOR BITWISEXOR
%left BITWISEAND
%left '%'
%left PLUS MINUS
%left TIMES '/'
 
383,15 → 384,17
 
expr:
value { $$ = $1; }
| MINUS value { $$ = new AST_BRANCH('-',new AST_NUMBER(0), $2); }
| expr PLUS value { $$ = new AST_BRANCH('+',$1,$3); }
| expr MINUS value { $$ = new AST_BRANCH('-',$1,$3); }
| expr TIMES value { $$ = new AST_BRANCH('*',$1,$3); }
| expr BOOLEANOR value { $$ = new AST_BRANCH('o',$1,$3); }
| expr BITWISEOR value { $$ = new AST_BRANCH('|',$1,$3); }
| expr BOOLEANAND value { $$ = new AST_BRANCH('a',$1,$3); }
| expr BITWISEAND value { $$ = new AST_BRANCH('&',$1,$3); }
| expr BITWISEXOR value { $$ = new AST_BRANCH('^',$1,$3); }
| MINUS value %prec TIMES { $$ = new AST_BRANCH('-',new AST_NUMBER(0), $2); }
| expr PLUS expr { $$ = new AST_BRANCH('+',$1,$3); }
| expr MINUS expr { $$ = new AST_BRANCH('-',$1,$3); }
| expr TIMES expr { $$ = new AST_BRANCH('*',$1,$3); }
| expr '/' expr { $$ = new AST_BRANCH('/',$1,$3); }
| expr '%' expr { $$ = new AST_BRANCH('%',$1,$3); }
| expr BOOLEANOR expr { $$ = new AST_BRANCH('o',$1,$3); }
| expr BITWISEOR expr { $$ = new AST_BRANCH('|',$1,$3); }
| expr BOOLEANAND expr { $$ = new AST_BRANCH('a',$1,$3); }
| expr BITWISEAND expr { $$ = new AST_BRANCH('&',$1,$3); }
| expr BITWISEXOR expr { $$ = new AST_BRANCH('^',$1,$3); }
| '(' expr ')' { $$ = $2; }
;
/* expr OR (|) value */
/asmdata.cpp
557,7 → 557,10
fprintf(fp, "\tTLINE OP = (Unrecognized, %d)\n", m_opcode);
break;
}
fprintf(fp, "\tTLINE COND = %d\n", m_cond);
if (m_cond == 0)
fprintf(fp, "\tTLINE COND = (Always)\n");
else
fprintf(fp, "\tTLINE COND = %s\n", zop_ccstr[m_cond&0x07]);
if (m_imm == NULL)
fprintf(fp, "\tTLINE imm = (NULL)\n");
else if (!m_imm->isdefined()) {
567,8 → 570,10
fprintf(fp, "\n");
} else
fprintf(fp, "\tTLINE imm = %d\n", m_imm->eval());
fprintf(fp, "\tTLINE opb = %d\n", m_opb);
fprintf(fp, "\tTLINE opa = %d\n", m_opa);
if (m_opb != ZPARSER::ZIP_Rnone)
fprintf(fp, "\tTLINE opb = %d\n", m_opb);
if (m_opa != ZPARSER::ZIP_Rnone)
fprintf(fp, "\tTLINE opa = %d\n", m_opa);
}
}
 
/zpp.l
46,6 → 46,8
#include <ctype.h>
#include <stdio.h>
#include <list>
#include <vector>
#include <assert.h>
 
using namespace std;
 
58,6 → 60,10
int ndef = 0, structno = 0;
char *structid = NULL;
void stb_define(const char *str);
bool stb_current(const char *str);
bool stb_hasargs(const char *str);
void stb_expand(FILE *fout, const char *str);
std::string stb_expand(const char *str);
void stb_args(const char *str);
void stb_macro(const char *value);
void stb_addmacro(const char *value);
91,6 → 97,7
<INITIAL,INDEF>^"#define"[ \t]+ { yy_push_state(DEF); }
/* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n { } */
<DEF>{IDDOT}/[^(] {
// fprintf(stderr, "Defining \'%s\'\n", yytext);
stb_define(yytext);
BEGIN DFV;
}
100,10 → 107,61
stb_args(yytext);
BEGIN DFV;
}
<DFV>[ \t]+ { /* Ignore initial spaces */ }
<DFV>[^ \t\n]* {/* Parse to end of line, get value for our define */
<DFV>^[ \t]+ { stb_macro(yytext); /* Replicate initial spaces */ }
<DFV>[ \t]+ { stb_macro(" "); /* Ignore all but one internal space */ }
<DFV>{ID} {/* Parse to end of line, get value for our define */
// fprintf(stderr, "%s may be a macro, line %d\n", yytext, yylineno);
if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
// fprintf(stderr, "Recursive MACRO!\n");
stb_macro(stb_getdefn(yytext));
} else {
// fprintf(stderr, "But it is not defined\n");
stb_macro(yytext);
}
}
<DFV>{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)] {
// fprintf(stderr, "%s may be a macro within a macro!\n", yytext);
if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
if (stb_hasargs(yytext)) {
std::string str = stb_expand(yytext);
stb_macro(str.c_str());
} else {
char *dup = strdup(yytext), *ptr;
ptr = strchr(dup, '(');
*ptr = '\0';
stb_macro(stb_getdefn(dup));
free(dup);
yyless(strchr(yytext,'(')-yytext);
}
} else {
char *dup = strdup(yytext), *ptr;
ptr = strchr(dup, '(');
*ptr = '\0';
stb_macro(stb_getdefn(dup));
free(dup);
yyless(strchr(yytext,'(')-yytext);
}
}
<DFV>[^a-zA-Z_0-9 \t\n]+ {/* Parse to end of line, get value for our define */
// fprintf(stderr, "A: Adding to macro %s\n", yytext);
stb_macro(yytext);
}
<DFV>0[xX][0-9A-Fa-f]+ {/* Get any hexadecimal constants */
// fprintf(stderr, "B: Adding to macro %s\n", yytext);
stb_macro(yytext);
}
<DFV>[0-9A-Fa-f]+[hH] {/* Get any hexadecimal constants */
// fprintf(stderr, "C: Adding to macro %s\n", yytext);
stb_macro(yytext);
}
<DFV>[0-7]+[oO] {/* Get any hexadecimal constants */
// fprintf(stderr, "D: Adding to macro %s\n", yytext);
stb_macro(yytext);
}
<DFV>[0-9]+ {/* Get any hexadecimal constants */
// fprintf(stderr, "E: Adding to macro %s\n", yytext);
stb_macro(yytext);
}
<DFV>[ \t]*((;|"//").*)?$ {/* Parse to end of line, get value for our define */
yy_pop_state();
}
159,6 → 217,26
else
fprintf(yyout, "%s", yytext);
}
<INITIAL,INDEF>{ID}([ \t]*) {
if (stb_isdefined(yytext))
fprintf(yyout, "%s", stb_getdefn(yytext));
else
fprintf(yyout, "%s", yytext);
}
<INITIAL,INDEF>{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)] {
// fprintf(stderr, "%s may be a macro!\n", yytext);
if (stb_isdefined(yytext)) {
if (stb_hasargs(yytext)) {
stb_expand(yyout, yytext);
} else {
fprintf(yyout, "%s", stb_getdefn(yytext));
yyless(strchr(yytext,'(')-yytext);
}
} else {
// fprintf(stderr, "But it is not defined\n");
fprintf(yyout, "%s", yytext);
}
}
<*>[ \t]*"//".*$ { /* Ignore (trailing) comment only lines */ }
<*>[ \t]*";".*$ { /* Ignore (trailing) comment only lines */ }
<*>"#warning".*$ { fprintf(stderr, "WARNING: %s\n", &yytext[8]); }
178,8 → 256,16
 
%%
 
class SYMTABLE_ACTION {
public:
// Types: 0 (end of actions), 1-X, argument number, <0 raw string
int m_type;
std::string m_str; // if m_type < 0, have m_str
};
 
class SYMTABLE_ENTRY {
private:
bool m_reduced;
std::string &trim(std::string &s) {
std::string::iterator ptr = s.end();
 
191,8 → 277,10
 
public:
std::string m_name, m_value, m_args;
std::vector<SYMTABLE_ACTION> m_actions;
SYMTABLE_ENTRY(const char *str) : m_name(str) {
trim(m_name);
m_reduced = false;
}
SYMTABLE_ENTRY &operator+=(const char *str) {
const char *start = str;
202,9 → 290,7
if (m_value.length()!=0)
m_value += " ";
 
std::string trimd(start);
trim(trimd);
m_value += trimd;
m_value += str;
 
/*
printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
220,6 → 306,153
const std::string &getdefn(void) {
return m_value;
}
 
bool hasargs(void) {
return (m_args.size()>0);
}
 
void reduce(void) {
if (m_reduced)
return;
 
// fprintf(stderr, "Reducing %s ( %s ) \n", m_name.c_str(), m_args.c_str());
std::vector<std::string> alist;
int i=0, bg, en;
do {
if ((m_args[i] == ',')||(m_args[i] == '('))
i++;
while((m_args[i])&&(isspace(m_args[i])))
i++;
bg = i;
while((m_args[i])&&(
(isalpha(m_args[i]))
||(m_args[i]==':')
||(m_args[i]=='_')
||(isdigit(m_args[i]))))
i++;
en = i;
while((m_args[i])&&(isspace(m_args[i])))
i++;
 
alist.push_back(m_args.substr(bg,en-bg));
// printf("Found argument %2ld of %s: \'%s\'\n",
// alist.size(),
// m_name.c_str(),
// m_args.substr(bg,en-bg).c_str());
} while((m_args[i])&&(m_args[i] == ','));
 
assert(m_args[i] == ')');
 
// Now that we know our arguments, lets look for these
// arguments in our macro definition
std::string building;
i = 0;
while(m_value[i]) {
int nxti = m_value.size(), nxtv;
for(int a=0; a<alist.size(); a++) {
const char *ptr;
ptr = strstr(m_value.c_str()+i, alist[a].c_str());
while((ptr)&&(ptr-m_value.c_str() < nxti)) {
int loc = ptr-m_value.c_str();
const char *pre = ptr-1;
const char *pst = ptr+alist[a].size();
if ((loc < nxti)&&(
(loc == i)
||((!isalpha(*pre)
&&(*pre != '_')
&&(*pre != ':')
&&(!isdigit(*pre)))))
&&((*pst=='\0')
||( (!isalpha(*pst))
&&(*pst != '_')
&&(*pst != ':')
&&(!isdigit(*pst)))))
{
nxti = loc;
nxtv = a;
break;
} else {
ptr = strstr(m_value.c_str()+loc, alist[a].c_str());
loc = ptr-m_value.c_str();
}
}
}
 
if (nxti < m_value.size()) {
// Found an argument!!
SYMTABLE_ACTION act;
if (nxti > i) {
act.m_type = -1;
act.m_str = m_value.substr(i,nxti-i);
// printf("ACTION: \'%s\'\n", act.m_str.c_str());
m_actions.push_back(act);
}
act.m_type = nxtv;
act.m_str = "";
m_actions.push_back(act);
// printf("ACTION[%2d]: \'%s\'\n", nxtv, alist[nxtv].c_str());
i = nxti+alist[nxtv].size();
} else break; // No more arguments
} if (i<m_value.size()) {
SYMTABLE_ACTION act;
act.m_type = -1;
act.m_str = m_value.substr(i);
// printf("ACTION: \'%s\'\n", act.m_str.c_str());
m_actions.push_back(act);
}
m_reduced = true;
}
 
std::string expand(std::string args) {
if (!m_reduced)
reduce();
std::vector<std::string> alist;
std::string result;
 
// printf("Expanding %s\n", args.c_str());
int i=0, bg, en, nest=-1;
do {
if ((args[i] == '(')||(args[i] == ',')) {
if (args[i] =='(')
nest++;
i++;
}
while((args[i])&&(isspace(args[i])))
i++;
bg = i;
while((args[i])&&(args[i] != ',')&&((args[i] != ')')||(nest != 0))) {
if (args[i] == '(')
nest++;
else if (args[i] == ')')
nest--;
i++;
} en = i-1;
while((en>0)&&(isspace(args[en])))
en--;
alist.push_back(args.substr(bg,en+1-bg));
// printf("Argument %2ld of %s maps to \'%s\'\n",
// alist.size(),
// m_name.c_str(),
// args.substr(bg,en+1-bg).c_str());
} while((args[i])&&(args[i] == ','));
 
// printf("At end, args[i] = \'%s\'\n", &args[i]);
assert(args[i] == ')');
 
// printf("Filling in %ld actions\n", m_actions.size());
for(i=0; i<m_actions.size(); i++) {
if((m_actions[i].m_type >= 0)&&(m_actions[i].m_type < alist.size()))
result += alist[m_actions[i].m_type].c_str();
else if (m_actions[i].m_type < 0)
result += m_actions[i].m_str.c_str();
// else {
// fprintf(fout, "m_type = %d, size = %ld\n", m_actions[i].m_type, alist.size());
// }
}
 
return result;
}
};
 
class SYMBOL_TABLE {
247,7 → 480,7
;
m_tbl.insert(i, v);
 
// printf("SYMS::Defining SYMBOL: \'%s\'\n", str);
// fprintf(stderr, "SYMS::Defining SYMBOL: \'%s\'\n", str);
}
 
bool defined(const char *str) {
281,6 → 514,12
fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
} (*i)->setargs(str);
}
bool hasargs(const char *name) {
TBLT::iterator i = lookup(name);
if (i == m_tbl.end()) {
return false;
} return (*i)->hasargs();
}
const char *getdefn(const char *name) {
TBLT::iterator i = lookup(name);
if (i == m_tbl.end()) {
288,6 → 527,13
return NULL;
} (*i)->getdefn().c_str();
}
 
std::string expand(const char *name, const char *ptr) {
TBLT::iterator i = lookup(name);
if (i==m_tbl.end())
return std::string("");
return (*i)->expand(std::string(ptr));
}
};
 
296,6 → 542,12
std::string last_define;
 
void stb_define(const char *str) {
/*
if (last_define.size()>0) {
fprintf(stderr, "LAST-DEFINE(%s): %s\n", last_define.c_str(),
stb_getdefn(last_define.c_str()));
} */
 
if (syms.defined(str)) {
fprintf(stderr, "WARNING! Symbol \'%s\' is already defined!\n", str);
syms.undefine(str);
318,7 → 570,19
}
 
bool stb_isdefined(const char *str) {
return syms.defined(str);
const char *ptr;
if ((ptr = strchr(str, '('))!=NULL) {
// fprintf(stderr, "Checking whether %s needs to be expanded\n",str);
char *dup = strdup(str), *chr;
chr = strchr(dup, '(');
*chr = '\0';
// fprintf(stderr, "\tLooking it up by the name \'%s\'\n", dup);
bool r = (syms.defined(dup));
free(dup);
return r;
} else {
return syms.defined(str);
}
}
 
const char *stb_getdefn(const char *str) {
325,6 → 589,47
return syms.getdefn(str);
}
 
bool stb_current(const char *str) {
return (strcmp(str, last_define.c_str())==0);
}
 
bool stb_hasargs(const char *str) {
const char *ptr;
if ((ptr = strchr(str, '('))!=NULL) {
char *dup = strdup(str), *chr;
chr = strchr(dup, '(');
*chr = '\0';
bool r = (syms.hasargs(dup));
// fprintf(stderr, "\t%s has %sarguments\n", dup, (r)?"":"no ");
free(dup);
return r;
} else {
return syms.hasargs(str);
}
}
 
std::string stb_expand(const char *macro) {
const char *ptr;
std::string str;
ptr = strchr(macro, '(');
assert(ptr);
ptr--;
while((ptr>macro)&&(isspace(*ptr)))
ptr--;
char *nam = strndup(macro, ptr+1-macro);
ptr = strchr(ptr, '(');
// fprintf(stderr, "Requesting an expansion of %s -- %s\n", nam, ptr);
str = syms.expand(nam, ptr);
free(nam);
 
return str;
}
 
void stb_expand(FILE *fout, const char *macro) {
std::string str = stb_expand(macro);
fprintf(fout, "%s", str.c_str());
}
 
class BUFSTACK {
public:
FILE *m_fp;
/zasm.l
189,6 → 189,7
"^" { return BITWISEXOR; }
":" { return COLON; }
"." { return DOT; }
"%" { return '%'; }
[ \t]+ { }
[(] { return '('; }
[)] { return ')'; }
/sys.i
107,8 → 107,8
 
; Now, some macros
#define PUSH(RG,SP) SUB 1,SP \
STO RG,-1(SP)
#define POP(RG,SP) LOD -1(SP),RG \
STO RG,1(SP)
#define POP(RG,SP) LOD 1(SP),RG \
ADD 1,SP
#define FJSR(LBL,RG) MOV __here__+2(PC),RG \
JMP LBL
115,76 → 115,76
#define FRET(RG) MOV RG,PC
#define JSR(LBL,RG) SUB 1,SP \
MOV __here__+3(PC),RG \
STO RG,-1(SP) \
STO RG,1(SP) \
JMP LBL \
ADD 1,SP
#define RET LOD -1(SP),PC
#define RET LOD 1(SP),PC
#define SAVE_USER_CONTEXT(DR,AR) \
MOV -16(uSP),AR \
MOV uPC,DR \
STO DR,-16(AR) \
STO DR,16(AR) \
MOV uCC,DR \
STO DR,-15(AR) \
STO DR,15(AR) \
MOV uSP,DR \
STO DR,-14(AR) \
STO DR,14(AR) \
MOV uR12,DR \
STO DR,-13(AR) \
STO DR,13(AR) \
MOV uR11,DR \
STO DR,-12(AR) \
STO DR,12(AR) \
MOV uR10,DR \
STO DR,-11(AR) \
STO DR,11(AR) \
MOV uR9,DR \
STO DR,-10(AR) \
STO DR,10(AR) \
MOV uR8,DR \
STO DR,-9(AR) \
STO DR,9(AR) \
MOV uR7,DR \
STO DR,-8(AR) \
STO DR,8(AR) \
MOV uR6,DR \
STO DR,-7(AR) \
STO DR,7(AR) \
MOV uR5,DR \
STO DR,-6(AR) \
STO DR,6(AR) \
MOV uR4,DR \
STO DR,-5(AR) \
STO DR,5(AR) \
MOV uR3,DR \
STO DR,-4(AR) \
STO DR,4(AR) \
MOV uR2,DR \
STO DR,-3(AR) \
STO DR,3(AR) \
MOV uR1,DR \
STO DR,-2(AR) \
STO DR,2(AR) \
MOV uR0,DR \
STO DR,-1(AR)
STO DR,1(AR)
#define RESTORE_USER_CONTEXT(DR,AR) \
LOD -1(AR),DR \
LOD 1(AR),DR \
MOV DR,uR0 \
LOD -2(AR),DR \
LOD 2(AR),DR \
MOV DR,uR1 \
LOD -3(AR),DR \
LOD 3(AR),DR \
MOV DR,uR2 \
LOD -4(AR),DR \
LOD 4(AR),DR \
MOV DR,uR3 \
LOD -5(AR),DR \
LOD 5(AR),DR \
MOV DR,uR4 \
LOD -6(AR),DR \
LOD 6(AR),DR \
MOV DR,uR5 \
LOD -7(AR),DR \
LOD 7(AR),DR \
MOV DR,uR6 \
LOD -8(AR),DR \
LOD 8(AR),DR \
MOV DR,uR7 \
LOD -9(AR),DR \
LOD 9(AR),DR \
MOV DR,uR8 \
LOD -10(AR),DR \
LOD 10(AR),DR \
MOV DR,uR9 \
LOD -11(AR),DR \
LOD 11(AR),DR \
MOV DR,uR10 \
LOD -12(AR),DR \
LOD 12(AR),DR \
MOV DR,uR11 \
LOD -13(AR),DR \
LOD 13(AR),DR \
MOV DR,uR12 \
LOD -14(AR),DR \
LOD 14(AR),DR \
MOV DR,uSP \
LOD -15(AR),DR \
LOD 15(AR),DR \
MOV DR,uCC \
LOD -16(AR),DR \
LOD 16(AR),DR \
MOV DR,uPC
#define READ_USER_TRAP(RG) \
MOV uCC,RG \
/Makefile
95,7 → 95,7
 
.PHONY: test
test: dumpd.txt
z.out: test.S zasm zdump zpp
z.out: test.S sys.i zasm zdump zpp
./zasm test.S -o z.out
dumpd.txt: z.out zdump
./zdump z.out > dumpd.txt

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.