Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/stag019/rgbds
Browse files Browse the repository at this point in the history
  • Loading branch information
bentley committed Nov 5, 2014
2 parents 82de716 + a64d725 commit c1213f5
Show file tree
Hide file tree
Showing 18 changed files with 299 additions and 15 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ yacc_pre := \

rgbasm_obj := \
src/asm/asmy.o \
src/asm/charmap.o \
src/asm/fstack.o \
src/asm/globlex.o \
src/asm/lexer.o \
Expand Down
18 changes: 18 additions & 0 deletions include/asm/charmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ASMOTOR_ASM_CHARMAP_H
#define ASMOTOR_ASM_CHARMAP_H

#define MAXCHARMAPS 512
#define CHARMAPLENGTH 8

struct Charmap {
int count;
char input[MAXCHARMAPS][CHARMAPLENGTH + 1];
char output[MAXCHARMAPS];
};

int readUTF8Char(char *destination, char *source);
void charmap_Sort();
int charmap_Add(char *input, UBYTE output);
int charmap_Convert(char **input);

#endif
3 changes: 3 additions & 0 deletions include/asm/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Section {
ULONG nBank;
struct Section *pNext;
struct Patch *pPatches;
struct Charmap *charmap;
UBYTE *tData;
};

Expand All @@ -20,12 +21,14 @@ void out_SetFileName(char *s);
void out_NewSection(char *pzName, ULONG secttype);
void out_NewAbsSection(char *pzName, ULONG secttype, SLONG org, SLONG bank);
void out_AbsByte(int b);
void out_AbsByteGroup(char *s, int length);
void out_RelByte(struct Expression * expr);
void out_RelWord(struct Expression * expr);
void out_PCRelByte(struct Expression * expr);
void out_WriteObject(void);
void out_Skip(int skip);
void out_BinaryFile(char *s);
void out_BinaryFileSlice(char *s, SLONG start_pos, SLONG length);
void out_String(char *s);
void out_AbsLong(SLONG b);
void out_RelLong(struct Expression * expr);
Expand Down
2 changes: 2 additions & 0 deletions include/asm/types.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef ASMOTOR_ASM_TYPES_H
#define ASMOTOR_ASM_TYPES_H

#ifndef _MAX_PATH
#define _MAX_PATH 512
#endif

typedef unsigned char UBYTE;
typedef signed char SBYTE;
Expand Down
2 changes: 2 additions & 0 deletions include/link/mylink.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef ASMOTOR_LINK_LINK_H
#define ASMOTOR_LINK_LINK_H

#ifndef _MAX_PATH
#define _MAX_PATH 512
#endif

#include "link/types.h"

Expand Down
2 changes: 2 additions & 0 deletions include/link/types.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef ASMOTOR_LINK_TYPES_H
#define ASMOTOR_LINK_TYPES_H

#ifndef _MAX_PATH
#define _MAX_PATH 512
#endif

typedef unsigned char UBYTE;
typedef signed char SBYTE;
Expand Down
204 changes: 204 additions & 0 deletions src/asm/charmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Copyright © 2013 stag019 <stag019@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "asm/asm.h"
#include "asm/charmap.h"
#include "asm/main.h"
#include "asm/output.h"

struct Charmap globalCharmap = {0};

extern struct Section *pCurrentSection;

int
readUTF8Char(char *destination, char *source)
{
int size;
UBYTE first;
first = source[0];

if(first >= 0xFC)
{
size = 6;
}
else if(first >= 0xF8)
{
size = 5;
}
else if(first >= 0xF0)
{
size = 4;
}
else if(first >= 0xE0)
{
size = 3;
}
else if(first >= 0xC0)
{
size = 2;
}
else if(first != '\0')
{
size = 1;
}
else
{
size = 0;
}
strncpy(destination, source, size);
destination[size] = 0;
return size;
}

int
charmap_Add(char *input, UBYTE output)
{
int i, input_length;
char temp1i[CHARMAPLENGTH + 1], temp2i[CHARMAPLENGTH + 1], temp1o = 0, temp2o = 0;

struct Charmap *charmap;

if(pCurrentSection)
{
if(pCurrentSection -> charmap)
{
charmap = pCurrentSection -> charmap;
}
else
{
if((charmap = (struct Charmap *) calloc(1, sizeof(struct Charmap))) == NULL)
{
fatalerror("Not enough memory for charmap");
}
pCurrentSection -> charmap = charmap;
}
}
else
{
charmap = &globalCharmap;
}

if(nPass == 2)
{
return charmap -> count;
}

if(charmap -> count > MAXCHARMAPS || strlen(input) > CHARMAPLENGTH)
{
return -1;
}

input_length = strlen(input);
if(input_length > 1)
{
i = 0;
while(i < charmap -> count + 1)
{
if(input_length > strlen(charmap -> input[i]))
{
memcpy(temp1i, charmap -> input[i], CHARMAPLENGTH + 1);
memcpy(charmap -> input[i], input, input_length);
temp1o = charmap -> output[i];
charmap -> output[i] = output;
i++;
break;
}
i++;
}
while(i < charmap -> count + 1)
{
memcpy(temp2i, charmap -> input[i], CHARMAPLENGTH + 1);
memcpy(charmap -> input[i], temp1i, CHARMAPLENGTH + 1);
memcpy(temp1i, temp2i, CHARMAPLENGTH + 1);
temp2o = charmap -> output[i];
charmap -> output[i] = temp1o;
temp1o = temp2o;
i++;
}
memcpy(charmap -> input[charmap -> count + 1], temp1i, CHARMAPLENGTH + 1);
charmap -> output[charmap -> count + 1] = temp1o;
}
else
{
memcpy(charmap -> input[charmap -> count], input, input_length);
charmap -> output[charmap -> count] = output;
}
return ++charmap -> count;
}

int
charmap_Convert(char **input)
{
struct Charmap *charmap;

char outchar[CHARMAPLENGTH + 1];
char *buffer;
int i, j, length;

if(pCurrentSection && pCurrentSection -> charmap)
{
charmap = pCurrentSection -> charmap;
}
else
{
charmap = &globalCharmap;
}

if((buffer = (char *) malloc(strlen(*input))) == NULL)
{
fatalerror("Not enough memory for buffer");
}

length = 0;
while(**input)
{
j = 0;
for(i = 0; i < charmap -> count; i++)
{
j = strlen(charmap -> input[i]);
if(memcmp(*input, charmap -> input[i], j) == 0)
{
outchar[0] = charmap -> output[i];
outchar[1] = 0;
break;
}
j = 0;
}
if(!j)
{
j = readUTF8Char(outchar, *input);
}
if(!outchar[0])
{
buffer[length++] = 0;
}
else
{
for(i = 0; outchar[i]; i++)
{
buffer[length++] = outchar[i];
}
}
*input += j;
}
*input = buffer;
return length;
}

2 changes: 1 addition & 1 deletion src/asm/fstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fstk_RunMacro(char *s)
pCurrentMacro = sym;
CurrentFlexHandle =
yy_scan_bytes(pCurrentMacro->pMacro,
pCurrentMacro->ulMacroSize);
strlen(pCurrentMacro->pMacro));
yy_switch_to_buffer(CurrentFlexHandle);
return (1);
} else
Expand Down
7 changes: 4 additions & 3 deletions src/asm/globlex.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,21 @@ ascii2bin(char *s)
ULONG
ParseFixedPoint(char *s, ULONG size)
{
char dest[256];
//char dest[256];
ULONG i = 0, dot = 0;

while (size && dot != 2) {
if (s[i] == '.')
dot += 1;

if (dot < 2) {
dest[i] = s[i];
//dest[i] = s[i];
size -= 1;
i += 1;
}
}

dest[i] = 0;
//dest[i] = 0;

yyunputbytes(size);

Expand Down Expand Up @@ -308,6 +308,7 @@ struct sLexInitString staticstrings[] = {
{"rsset", T_POP_RSSET},

{"incbin", T_POP_INCBIN},
{"charmap", T_POP_CHARMAP},

{"fail", T_POP_FAIL},
{"warn", T_POP_WARN},
Expand Down
12 changes: 7 additions & 5 deletions src/asm/lexer.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include "asm/asm.h"
#include "asm/lexer.h"
#include "asm/types.h"
Expand All @@ -8,11 +15,6 @@

#include "asmy.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct sLexString {
char *tzName;
ULONG nToken;
Expand Down
2 changes: 1 addition & 1 deletion src/asm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
*/

#define _XOPEN_SOURCE 500
#include <math.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down
10 changes: 10 additions & 0 deletions src/asm/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string.h>

#include "asm/asm.h"
#include "asm/charmap.h"
#include "asm/output.h"
#include "asm/symbol.h"
#include "asm/mylink.h"
Expand Down Expand Up @@ -637,6 +638,7 @@ out_FindSection(char *pzName, ULONG secttype, SLONG org,
pSect->nBank = bank;
pSect->pNext = NULL;
pSect->pPatches = NULL;
pSect->charmap = NULL;
pPatchSymbols = NULL;

if ((pSect->tData = malloc(SECTIONCHUNK)) != NULL) {
Expand Down Expand Up @@ -709,6 +711,14 @@ out_AbsByte(int b)
nPC += 1;
pPCSymbol->nValue += 1;
}

void
out_AbsByteGroup(char *s, int length)
{
checkcodesection(length);
while (length--)
out_AbsByte(*s++);
}
/*
* RGBAsm - OUTPUT.C - Outputs an objectfile
*
Expand Down
1 change: 1 addition & 0 deletions src/asm/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
*/

#define _XOPEN_SOURCE 500
#include <assert.h>
#include <stdio.h>
#include <string.h>
Expand Down
Loading

0 comments on commit c1213f5

Please sign in to comment.