Skip to content

Commit

Permalink
Merge pull request #5 from flamecyclone/master
Browse files Browse the repository at this point in the history
Added new commands: .str and some code optimizations
  • Loading branch information
ClusterM committed Aug 7, 2023
2 parents 82f59a3 + 6f7e713 commit 92ee265
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 12 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,16 @@ Other 'special' parameters can be used, here's a list of all the possible parame
represent the page index.

DB - Store one or more data bytes at the current location.


STR - Stores a string, the first byte is the length of the string,
followed by the string content,
The effect is equivalent to . DB is preceded with a length,
here's a small example:
;use DB specified a length + string:
DB 12,"Hello World!"
;can be replaced with STR:
STR "Hello World!"

DW - Store data words.

BYTE - Same as DB.
Expand Down
176 changes: 175 additions & 1 deletion source/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ char pseudo_flag[] = {
0x0F, 0x0F, 0x0F, 0x0C, 0x0C, 0x0C, 0x0C, 0x0F, 0x0F, 0x0F, // 30 - 39
0x0F, 0x0F, 0x0C, 0x0C, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x04, // 40 - 49
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // P_INESPRGRAM - 50, P_INESPRGNVRAM - 51, P_INESCHRRAM - 52, P_INESCHRNVRAM - 53, P_INESSUBMAP - 54, P_INESBAT - 55, P_INESTIM - 56
0x0F // P_SEQU 57
0x0C, 0x0F // P_SEQU 58
};


Expand Down Expand Up @@ -337,6 +337,180 @@ do_dw(int *ip)
println();
}

/* ----
* do_str()
* ----
* .str pseudo
*/

void
do_str(int *ip)
{
unsigned char c;
unsigned char str_len = 0;
int ip_tmp = 0;

/* define label */
labldef(loccnt, 1);

/* output infos */
data_loccnt = loccnt;
data_level = 2;

/* skip spaces */
while (isspace((int)prlnbuf[++(*ip)]));

ip_tmp = *ip;
/* get string length */
for (;;) {
/* ASCII string */
if (prlnbuf[*ip] == '\"') {
for (;;) {
c = prlnbuf[++(ip_tmp)];
if (c == '\"')
break;
if (c == '\0') {
error("Unterminated ASCII string!");
return;
}
if (c == '\\') {
c = prlnbuf[++(ip_tmp)];
switch(c) {
case 'r':
c = '\r';
break;
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
}
}

/* update length counter */
str_len++;
}
ip_tmp++;
}
/* bytes */
else {
/* get a byte */
if (!evaluate(&ip_tmp, 0))
return;

/* update length counter */
str_len++;

/* check byte on last pass */
if (pass == LAST_PASS) {
/* check for overflow */
if ((value > 0xFF) && (value < 0xFFFFFF80)) {
error("Overflow error!");
return;
}
}
}

/* check if there's another byte */
c = prlnbuf[ip_tmp++];

if (c != ',')
break;
}

/* store string length on first btye */
if (str_len > 0) {
putbyte(loccnt, str_len);
loccnt++;
}

/* get bytes */
for (;;) {
/* ASCII string */
if (prlnbuf[*ip] == '\"') {
for (;;) {
c = prlnbuf[++(*ip)];
if (c == '\"')
break;
if (c == '\0') {
error("Unterminated ASCII string!");
return;
}
if (c == '\\') {
c = prlnbuf[++(*ip)];
switch(c) {
case 'r':
c = '\r';
break;
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
}
}
/* store char on last pass */
if (pass == LAST_PASS)
putbyte(loccnt, c);

/* update location counter */
loccnt++;
}
(*ip)++;
}
/* bytes */
else {
/* get a byte */
if (!evaluate(ip, 0))
return;

/* update location counter */
loccnt++;

/* store byte on last pass */
if (pass == LAST_PASS) {
/* check for overflow */
if ((value > 0xFF) && (value < 0xFFFFFF80)) {
error("Overflow error!");
return;
}

/* store byte */
putbyte(loccnt - 1, value);
}
}

/* check if there's another byte */
c = prlnbuf[(*ip)++];

if (c != ',')
break;
}

/* check error */
if (c != ';' && c != '\0') {
error("Syntax error!");
return;
}

/* size */
if (lablptr) {
lablptr->data_type = P_DB;
lablptr->data_size = loccnt - data_loccnt;
}
else {
if (lastlabl) {
if (lastlabl->data_type == P_DB)
lastlabl->data_size += loccnt - data_loccnt;
}
}

/* output line */
if (pass == LAST_PASS)
println();
}

/* ----
* do_equ()
Expand Down
8 changes: 5 additions & 3 deletions source/defs.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define MAX_BANKS 4096
#define BANK_SIZE 8192

/* path separator */
#if defined(DJGPP) || defined(MSDOS) || defined(WIN32)
Expand All @@ -14,9 +15,9 @@
#define MACHINE_NES 1

/* reserved bank index */
#define RESERVED_BANK (MAX_BANKS - 0x0F)
#define PROC_BANK (MAX_BANKS - 0x0F + 1)
#define GROUP_BANK (MAX_BANKS - 0x0F + 2)
#define RESERVED_BANK (MAX_BANKS - 0x10)
#define PROC_BANK (MAX_BANKS - 0x10 + 1)
#define GROUP_BANK (MAX_BANKS - 0x10 + 2)

/* tile format for encoder */
#define CHUNKY_TILE 1
Expand Down Expand Up @@ -106,6 +107,7 @@
#define P_INESBAT 55 // .inesbat
#define P_INESTIM 56 // .inestim
#define P_SEQU 57 // .sequ
#define P_STR 58 // .str

/* symbol flags */
#define MDEF 3 /* multiply defined */
Expand Down
4 changes: 2 additions & 2 deletions source/externs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern unsigned char rom[MAX_BANKS][8192];
extern unsigned char map[MAX_BANKS][8192];
extern unsigned char rom[MAX_BANKS][BANK_SIZE];
extern unsigned char map[MAX_BANKS][BANK_SIZE];
extern char bank_name[MAX_BANKS][64];
extern int bank_loccnt[4][256];
extern int bank_page[4][256];
Expand Down
4 changes: 3 additions & 1 deletion source/inst.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct t_opcode base_inst[57] = {
};

/* pseudo instruction table */
struct t_opcode base_pseudo[77] = {
struct t_opcode base_pseudo[79] = {
{NULL, "=", do_equ, PSEUDO, P_EQU, 0},

{NULL, "BANK", do_bank, PSEUDO, P_BANK, 0},
Expand Down Expand Up @@ -102,6 +102,7 @@ struct t_opcode base_pseudo[77] = {
{NULL, "RS", do_rs, PSEUDO, P_RS, 0},
{NULL, "WORD", do_dw, PSEUDO, P_DW, 0},
{NULL, "ZP", do_section, PSEUDO, P_ZP, S_ZP},
{NULL, "STR", do_str, PSEUDO, P_STR, 0},

{NULL, ".BANK", do_bank, PSEUDO, P_BANK, 0},
{NULL, ".BSS", do_section, PSEUDO, P_BSS, S_BSS},
Expand Down Expand Up @@ -140,6 +141,7 @@ struct t_opcode base_pseudo[77] = {
{NULL, ".RS", do_rs, PSEUDO, P_RS, 0},
{NULL, ".WORD", do_dw, PSEUDO, P_DW, 0},
{NULL, ".ZP", do_section, PSEUDO, P_ZP, S_ZP},
{NULL, ".STR", do_str, PSEUDO, P_STR, 0},
{NULL, NULL, NULL, 0, 0, 0}
};

4 changes: 2 additions & 2 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ main(int argc, char **argv)
}

/* clear the ROM array */
memset(rom, zero_fill ? 0 : 0xff, 8192 * 128);
memset(map, zero_fill ? 0 : 0xff, 8192 * 128);
memset(rom, zero_fill ? 0 : 0xff, sizeof(rom));
memset(map, zero_fill ? 0 : 0xff, sizeof(map));

/* fill the instruction hash table */
addinst(base_inst);
Expand Down
1 change: 1 addition & 0 deletions source/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void do_mlist(int *ip);
void do_nolist(int *ip);
void do_nomlist(int *ip);
void do_db(int *ip);
void do_str(int *ip);
void do_dw(int *ip);
void do_equ(int *ip);
void do_sequ(int *ip);
Expand Down
4 changes: 2 additions & 2 deletions source/vars.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
unsigned char rom[MAX_BANKS][8192];
unsigned char map[MAX_BANKS][8192];
unsigned char rom[MAX_BANKS][BANK_SIZE];
unsigned char map[MAX_BANKS][BANK_SIZE];
char bank_name[MAX_BANKS][64];
int bank_loccnt[4][256];
int bank_page[4][256];
Expand Down

0 comments on commit 92ee265

Please sign in to comment.