1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include "spasm.h"
#include "fcaseopen.h"
int rawArgc = 0;
char rawArgv[64][128];
char *curText;
char *curLine;
FILE *asmOut;
int line_number;
char *fileBuffer;
unsigned int fileBufferSize;
int open_case_sensitive = 0;
static void titlecard(void)
{
printf("nv-spASM version 0.34.1 (c) 2014-2015 nextvolume\n");
}
static void usage(char *prog_name)
{
titlecard();
printf("Assembler for the Sony PlayStation\n");
printf("\n");
printf("Usage: %s [options] infile outfile\n", prog_name);
printf("\n");
printf("Options: -b create binary file instead of PS-X EXE\n");
printf(" -N do not pad executable\n");
printf(" -S be case sensitive on file paths\n");
printf("\n");
}
FILE *spasm_fopen(const char *path, const char *mode)
{
if(open_case_sensitive)
return fopen(path, mode);
return fcaseopen(path, mode);
}
int main(int argc, char *argv[])
{
int sz;
int farg=1;
int bfile=0;
int no_newline=0;
int no_pad=0;
if(argc < 3)
{
usage(argv[0]);
return EXIT_SUCCESS;
}
while(argv[farg][0] == '-')
{
if(strcmp(argv[farg], "-b") == 0)
bfile = 1; // Generate binary file
else if(strcmp(argv[farg], "-N") == 0)
no_pad = 1;
else if(strcmp(argv[farg], "-S") == 0)
open_case_sensitive = 1;
else
{
usage(argv[0]);
return EXIT_FAILURE;
}
farg++;
}
FILE *f = spasm_fopen(argv[farg], "rb");
if(!f)
{
printf("Could not open file %s for reading! Exiting.\n", argv[farg]);
return EXIT_FAILURE;
}
fseek(f, 0, SEEK_END);
sz = ftell(f);
fseek(f, 0, SEEK_SET);
fileBufferSize = sz + 2; // two newline bytes + two NUL bytes
fileBuffer = malloc(fileBufferSize);
fread(fileBuffer, sizeof(char), sz, f);
if(fileBuffer[sz-1] != '\n')
no_newline = 1;
fileBuffer[sz] = '\n';
fileBuffer[sz+1] = '\0';
//fileBuffer = process_includes(fileBuffer);
fclose(f);
asmOut = fopen(argv[farg+1], "wb");
if(!asmOut)
{
printf("Could not open file %s for writing! Exiting.\n", argv[farg+1]);
return EXIT_FAILURE;
}
startAddress = 0x80010000;
titlecard();
if(no_newline)
printf("Warning: No newline found at end of file.\n");
printf("\nPass 1\n");
codegen_init();
fileBuffer = spasm_parser(fileBuffer, -1);
fileBuffer = spasm_parser(fileBuffer, 0);
/*if(!org_found)
{
printf("Warning: no ORG directive found... defaulting to $80010000\n");
startAddress = 0x80010000;
}*/
curPc = startAddress;
printf("Pass 2\n");
printf("Writing output file %s ...\n", argv[farg+1]);
if(!bfile)
OUTSEEK(0x800);
fileBuffer = spasm_parser(fileBuffer, 1);
if(!bfile)
{
OUTSEEK(0x0);
OUTSTRING("\"PS-X EXE\""); // PSX-EXE magic
OUTSEEK(0x10);
OUTWORD(startAddress); // Initial program counter
OUTSEEK(0x18);
OUTWORD(startAddress); // Text section start address
OUTSEEK(0x30);
OUTWORD(0x801FFFF0); // Initial stack pointer
sz = OUTSIZE();
if(!no_pad && (sz & 2047)) // Pad executable
{
sz |= 2047;
sz++;
OUTSEEK(sz-1);
OUTBYTE(0);
}
OUTSEEK(0x1C);
OUTWORD(sz - 0x800); // Output size of text section
}
printf("\nAssembly complete\n\n");
fclose(asmOut);
return EXIT_SUCCESS;
}
|