1 /*
2 	Copyright (C) 2018  Clipsey
3 
4 	This program is free software; you can redistribute it and/or
5 	modify it under the terms of the GNU General Public License
6 	as published by the Free Software Foundation; either version 2
7 	of the License, or (at your option) any later version.
8 
9 	This program is distributed in the hope that it will be useful,
10 	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 	GNU General Public License for more details.
13 
14 	You should have received a copy of the GNU General Public License
15 	along with this program; if not, write to the Free Software
16 	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 module criscfe;
19 import crisc;
20 import std.stdio;
21 import std.string;
22 import std.array;
23 import std.conv;
24 import std.file;
25 import std.algorithm;
26 
27 enum licenseTextHeader = "CRISC ASM v. 1.0, Copyright (C) 2018 Clipsey
28 CRISC ASM is licensed under the GPLv2 license.
29 Run with -co for details.
30 ";
31 
32 enum licenseTextHeaderExec = "CRISC EXEC v. 1.0, Copyright (C) 2018 Clipsey
33 CRISC EXEC is licensed under the GPLv2 license.
34 Run with -co for details.
35 ";
36 
37 enum licenseText = "Copyright (C) 2018  Clipsey
38 
39 This program is free software; you can redistribute it and/or
40 modify it under the terms of the GNU General Public License
41 as published by the Free Software Foundation; either version 2
42 of the License, or (at your option) any later version.
43 
44 This program is distributed in the hope that it will be useful,
45 but WITHOUT ANY WARRANTY; without even the implied warranty of
46 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47 GNU General Public License for more details.
48 
49 You should have received a copy of the GNU General Public License
50 along with this program; if not, write to the Free Software
51 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
52 ";
53 
54 class SysCallPrtC : SysCall {
55 
56 	this() {
57 		super("prtc");
58 	}
59 
60 	this(CPU valueptr) {
61 		super("prtc", valueptr);
62 	}
63 
64 	public override void execute() {
65 		version(CPU) {
66 			char c = cast(char)valueptr.pop();
67 			import std.stdio;
68 			write(c);
69 		}
70 	}
71 }
72 
73 void main(string[] args)
74 {
75 	version(CPU) {
76 		if (args.length <= 1) {
77 			writeln(licenseTextHeaderExec);
78 			writeln("Usage
79 \tcriscexec <file>");
80 			return;
81 		}
82 		if (args[1] == "--copyright" || args[1] == "-co") {
83 			writeln(licenseText);
84 			return;
85 		}
86 		auto processor = new CPU(cast(ubyte[])read(args[1]), 32, 512);
87 		processor.PushSyscalls([
88 			new SysCallPrtC(processor)]
89 		);
90 		while (processor.running) {
91 			processor.runCycle();   
92 		}
93 	}
94 
95 	version (ASM) {
96 		bool verbose = false;
97 		bool link = false;
98 		bool conv = false;
99 
100 		string linkTmp = "";
101 		string firstFile = "";
102 		string outFile = "";
103 
104 		writeln(licenseTextHeader);
105 
106 		foreach (file; args[1..$]) {
107 			if (file.startsWith("-")) {
108 				if (file == "--verbose" || file == "-v") {
109 					verbose = true;
110 				}
111 				if (file == "--link" || file == "-l") {
112 					link = true;
113 				}
114 				if (file == "--copyright" || file == "-co") {
115 					writeln(licenseText);
116 					return;
117 				}
118 				if (file == "--conv" || file == "-c") {
119 					conv = true;
120 				}
121 			} else {
122 				if (firstFile == "") firstFile = file;
123 				if (link) {
124 					linkTmp ~= "\n"~readText(file);
125 				} else {
126 					outFile = file[0..$-3]~"bin";
127 					File output = File(outFile, "w");
128 					auto compiler = new Compiler(true, [new SysCallPrtC()]);
129 					output.rawWrite(compiler.compile(readText(file)));
130 					if (verbose) compiler.printLabels();
131 					output.close();
132 				}
133 			}
134 		}
135 
136 		if (link) {
137 			outFile  = firstFile[0..$-3]~"bin";
138 			File output = File(outFile, "w");
139 			auto compiler = new Compiler(true, [new SysCallPrtC()]);
140 			output.rawWrite(compiler.compile(linkTmp));
141 			if (verbose) compiler.printLabels();
142 			output.close();
143 		}
144 		
145 		if (conv) 
146 			writeln(binToASMDESC(cast(ubyte[])read(outFile)));
147 
148 		if (args.length <= 1) {
149 			writeln("Usage
150 criscasm (flags) <files>
151 Flags
152 \t--verbose/-v       | Verbose mode
153 \t--link/-l          | Link asm files together (output will be named after the first file)
154 \t--conv/-c          | Assemble and view human-readable conversion of assembly
155 \t--copyright/-co    | View license.");
156 		}
157 	}
158 }