1 module criscfe;
2 import crisc;
3 import std.stdio;
4 import std.string;
5 import std.array;
6 import std.conv;
7 import std.file;
8 import std.algorithm;
9 
10 class SysCallPrtC : SysCall {
11 
12 	this() {
13 		super("prtc");
14 	}
15 
16 	this(CPU valueptr) {
17 		super("prtc", valueptr);
18 	}
19 
20 	public override void execute() {
21 		version(CPU) {
22 			char c = cast(char)valueptr.pop();
23 			import ncurses;
24 			switch(c) {
25 				case(127):
26 					int x;
27 					int y;
28 					int w;
29 					int h;
30 					getyx(stdscr, &x, &y);
31 					getmaxyx(stdscr, &w, &h);
32 					
33 					x--;
34 					if ( x < 0 ) {
35 						x = w-1;
36 						y--;
37 					}
38 					if ( y < 0 ) {
39 						y = 0;
40 						x = 0;
41 					}
42 					mvdelch(y, x);
43 				break;
44 				default:		printw([c, '\0'].ptr);		break;
45 			}
46 			refresh();
47 		}
48 	}
49 }
50 
51 class SysCallReadC : SysCall {
52 
53 	this() {
54 		super("rdc");
55 	}
56 
57 	this(CPU valueptr) {
58 		super("rdc", valueptr);
59 		version(CPU) {
60 			import ncurses;
61 			initscr();
62 			noecho();
63 			//keypad(stdscr, true);
64 			//raw();
65 		}
66 	}
67 
68 	public override void execute() {
69 		version(CPU) {
70 			import ncurses;
71 			char code = cast(char)getch();
72 			valueptr.push(cast(size_t)code);
73 		}
74 	}
75 }
76 
77 void main(string[] args)
78 {
79 	version(CPU) {
80 		auto processor = new CPU(cast(ubyte[])read(args[1]), 32, 512);
81 		processor.PushSyscalls([
82 			new SysCallPrtC(processor), 
83 			new SysCallReadC(processor)]
84 		);
85 		while (processor.running) {
86 			processor.runCycle();   
87 		}
88 		if (args.length <= 1) {
89 			writeln("Usage
90 \tcriscexec <file>");
91 		}
92 	}
93 
94 	version (ASM) {
95 		bool verbose = false;
96 		bool link = false;
97 		bool conv = false;
98 
99 		string linkTmp = "";
100 		string firstFile = "";
101 		string outFile = "";
102 
103 		foreach (file; args[1..$]) {
104 			if (file.startsWith("-")) {
105 				if (file == "--verbose" || file == "-v") {
106 					verbose = true;
107 				}
108 				if (file == "--link" || file == "-l") {
109 					link = true;
110 				}
111 				if (file == "--conv" || file == "-c") {
112 					conv = true;
113 				}
114 			} else {
115 				if (firstFile == "") firstFile = file;
116 				if (link) {
117 					linkTmp ~= "\n"~readText(file);
118 				} else {
119 					outFile = file[0..$-3]~"bin";
120 					File output = File(outFile, "w");
121 					auto compiler = new Compiler(true, [new SysCallPrtC(), new SysCallReadC()]);
122 					output.rawWrite(compiler.compile(readText(file)));
123 					if (verbose) compiler.printLabels();
124 					output.close();
125 				}
126 			}
127 		}
128 
129 		if (link) {
130 			outFile  = firstFile[0..$-3]~"bin";
131 			File output = File(outFile, "w");
132 			auto compiler = new Compiler(true, [new SysCallPrtC(), new SysCallReadC()]);
133 			output.rawWrite(compiler.compile(linkTmp));
134 			if (verbose) compiler.printLabels();
135 			output.close();
136 		}
137 		
138 		if (conv) 
139 			writeln(binToASMDESC(cast(ubyte[])read(outFile)));
140 
141 		if (args.length <= 1) {
142 			writeln("Usage
143 criscasm (flags) <files>
144 Flags
145 \t--verbose/-v | verbose mode
146 \t--link/-l    | link asm files together (output will be named after the first file)
147 \t--conv/-c    | assemble and view human-readable conversion of assembly");
148 		}
149 	}
150 }