Skip to content

Commit 4530500

Browse files
committed
Program now takes a filename as a cli argument when not testing. Removed
the input.vm file as it was garbage.
1 parent d2ccb5d commit 4530500

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

input.vm

-2 Bytes
Binary file not shown.

main.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
#include "string.h"
55

66
#ifdef TEST
7-
const char* INPUT_FILE = "test.vm";
87
const bool TESTING = true;
98
#else
10-
const char* INPUT_FILE = "input.vm";
119
const bool TESTING = false;
1210
#endif
11+
const char* TEST_FILE = "test.vm";
1312

1413
#ifdef DEBUG
1514
const bool DEBUGGING = true;
@@ -107,7 +106,6 @@
107106
BIN_Q_START(a,b); \
108107
sp+=3; \
109108
mem[sp] = ((SQWORD)(b) op (SQWORD)(a))
110-
;
111109

112110
// Types //
113111
typedef unsigned char BYTE;
@@ -131,6 +129,7 @@ const size_t EXIT_SUF = 5;
131129
const size_t EXIT_POB = 6;
132130
const size_t EXIT_OP = 7;
133131
const size_t EXIT_STR = 8;
132+
const size_t EXIT_ARGS = 9;
134133

135134
// OP codes //////////////////////////////////////////////////////////////////
136135
enum OP{ HALT=0x00,
@@ -209,6 +208,10 @@ static inline void set_qword(WORD* mem, ADDR addr, QWORD data) {
209208
// I/O ///////////////////////////////////////////////////////////////////////
210209
size_t read_program(WORD* mem, const char* filename) {
211210
FILE* fptr = fopen(filename, "rb");
211+
if (fptr == NULL) {
212+
fprintf(stderr, "Error: Input file could not be open: %s\n", filename);
213+
exit(EXIT_FILE);
214+
}
212215

213216
fseek(fptr, 0, SEEK_END);
214217
size_t prog_len = (size_t) ftell(fptr);
@@ -247,7 +250,7 @@ void debug_memory(WORD* mem, ADDR start, ADDR end) {
247250
}
248251

249252
// Main //////////////////////////////////////////////////////////////////////
250-
int main() {
253+
int main( int argc, char *argv[] ) {
251254
// Some variables we need
252255
ADDR sp, bp, pc, ra, fp, prog_len, brk;
253256
WORD* mem;
@@ -260,8 +263,16 @@ int main() {
260263
}
261264

262265
// Read the program file as bytes into memory starting at PSTART
263-
// TODO use an argument for the program filename
264-
prog_len = (ADDR)read_program(mem, INPUT_FILE);
266+
if (TESTING) {
267+
prog_len = (ADDR)read_program(mem, TEST_FILE);
268+
} else if (argc >= 2) {
269+
prog_len = (ADDR)read_program(mem, argv[1]);
270+
} else {
271+
fprintf(stderr, "Error: File name missing\n");
272+
fprintf(stderr, "Usage: lt64 <input file name>\n");
273+
exit(EXIT_ARGS);
274+
}
275+
265276

266277
// Set up the address variables
267278
pc = P_START;

0 commit comments

Comments
 (0)