Browse code

Created a debugger

neauoire authored on 23/03/2021 02:04:31
Showing 14 changed files
... ...
@@ -1,25 +1,15 @@
1 1
 # Uxn
2 2
 
3
-A [stack-based VM](https://wiki.xxiivv.com/site/uxn.html), written in ANSI C.
3
+A [stack-based VM](https://wiki.xxiivv.com/site/uxn.html), written in ANSI C. 
4 4
 
5
-## Setup
5
+## Build
6 6
 
7
-If you wish to build your own emulator, you can create a new instance of Uxn like:
7
+To build the Uxn emulator, you must have [SDL2](https://wiki.libsdl.org/).
8 8
 
9
-```
10
-#include "uxn.h"
11
-
12
-Uxn u;
13
-
14
-if(!bootuxn(&u))
15
-	return error("Boot", "Failed");
16
-if(!loaduxn(&u, argv[1]))
17
-	return error("Load", "Failed");
18
-if(!init())
19
-	return error("Init", "Failed");
20
-
21
-evaluxn(u, u->vreset); /* Once on start */
22
-evaluxn(u, u->vframe); /* Each frame
9
+```sh
10
+./build.sh 
11
+	--debug # Add debug flags to compiler
12
+	--cli # Run rom without graphics
23 13
 ```
24 14
 
25 15
 ## Uxambly
... ...
@@ -1,28 +1,42 @@
1 1
 #!/bin/bash
2 2
 
3
-# Create bin folder
4
-mkdir -p bin
3
+echo "Formatting.."
4
+clang-format -i src/assembler.c
5
+clang-format -i src/uxn.h
6
+clang-format -i src/uxn.c
7
+clang-format -i src/emulator.c
8
+clang-format -i src/debugger.c
5 9
 
6
-# Assembler
7
-clang-format -i assembler.c
10
+echo "Cleaning.."
8 11
 rm -f ./bin/assembler
12
+rm -f ./bin/emulator
13
+rm -f ./bin/debugger
9 14
 rm -f ./bin/boot.rom
10
-cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined assembler.c -o bin/assembler
11 15
 
12
-# Core
13
-clang-format -i uxn.h
14
-clang-format -i uxn.c
16
+echo "Building.."
17
+mkdir -p bin
18
+if [ "${1}" = '--debug' ]; 
19
+then
20
+	echo "[debug]"
21
+    cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined src/assembler.c -o bin/assembler
22
+	cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined src/uxn.c src/emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator
23
+    cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined src/uxn.c src/debugger.c -o bin/debugger
24
+else
25
+	cc src/assembler.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -o bin/assembler
26
+	cc src/uxn.c src/debugger.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -o bin/debugger
27
+	cc src/uxn.c src/emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
28
+fi
15 29
 
16
-# Emulator
17
-clang-format -i emulator.c
18
-rm -f ./bin/emulator
19
-cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator
20
-# cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
30
+echo "Assembling.."
31
+./bin/assembler projects/examples/dev.console.usm bin/boot.rom
21 32
 
22
-# Emulator(CLI)
23
-clang-format -i emulator-cli.c
24
-rm -f ./bin/emulator-cli
33
+echo "Running.."
34
+if [ "${2}" = '--cli' ]; 
35
+then
36
+	echo "[cli]"
37
+	./bin/debugger bin/boot.rom
38
+else
39
+	./bin/emulator bin/boot.rom
40
+fi
25 41
 
26
-# run
27
-./bin/assembler projects/software/nasu.usm bin/boot.rom
28
-./bin/emulator bin/boot.rom
42
+echo "Done."
29 43
\ No newline at end of file
30 44
deleted file mode 100644
... ...
@@ -1,18 +0,0 @@
1
-( tests/cond )
2
-
3
-|0100 @RESET 
4
-	
5
-	#1234 POP2 
6
-	#00 DUP2?
7
-
8
-BRK
9
-
10
-
11
-
12
-|c000 @FRAME
13
-|d000 @ERROR 
14
-
15
-|FF00 ;Console { pad 8 char 1 byte 1 short 2 }
16
-
17
-|FFF0 .RESET .FRAME .ERROR ( vectors )
18
-|FFF8 [ 13fd 1ef3 1bf2 ] ( palette )
19 0
\ No newline at end of file
20 1
deleted file mode 100644
... ...
@@ -1,71 +0,0 @@
1
-( tests/draw )
2
-
3
-%RTN   { JMP2r }
4
-%RTN?  { JMP2r? }
5
-%ABS { DUP #07 SHR #ff SWP MUL? }
6
-%ABS2 { DUP2 #000f SFT2 #ffff SWP2 SWP POP MUL2? } 
7
-
8
-;cursor { x 2 y 2 }
9
-;a { x 2 y 2 }
10
-;b { x 2 y 2 }
11
-;s { x 2 y 2 }
12
-;d { x 2 y 2 }
13
-;err { short 2 }
14
-;err2 { short 2 }
15
-;i { byte 1 }
16
-;color { byte 1 }
17
-
18
-|0100 @RESET 
19
-	
20
-	#0020 #0020 #0070 #0080 #01 ,draw-line JSR2
21
-	#0020 #0080 #0070 #0030 #02 ,draw-line JSR2
22
-	#00a0 #0020 #0050 #00b0 #03 ,draw-line JSR2
23
-	#00b0 #0090 #0030 #0010 #01 ,draw-line JSR2
24
-
25
-BRK
26
-
27
-@draw-line ( x1 y1 x2 y2 )
28
-	
29
-	=color
30
-	=b.y =b.x =a.y =a.x
31
-	~b.x ~a.x SUB2 ABS2 =d.x
32
-	~b.y ~a.y SUB2 ABS2 #0000 SWP2 SUB2 =d.y
33
-	#ffff #00 ~a.x ~b.x LTS2 #0002 MUL2 ADD2 =s.x 
34
-	#ffff #00 ~a.y ~b.y LTS2 #0002 MUL2 ADD2 =s.y 
35
-	~d.x ~d.y ADD2 =err
36
-
37
-	$loop
38
-
39
-		~a.x =Screen.x ~a.y =Screen.y ~color =Screen.color
40
-		,$end ~a.x ~b.x EQU2 ~a.y ~b.y EQU2 #0101 EQU2 JMP2?
41
-		~err #0002 MUL2 =err2
42
-
43
-		,$skipy ~err2 ~d.y LTS2 JMP2?
44
-			~err ~d.y ADD2 =err
45
-			~a.x ~s.x ADD2 =a.x
46
-		$skipy
47
-
48
-		,$skipx ~err2 ~d.x GTS2 JMP2?
49
-			~err ~d.x ADD2 =err
50
-			~a.y ~s.y ADD2 =a.y
51
-		$skipx
52
-
53
-		,$loop JMP2
54
-
55
-	$end
56
-
57
-RTN
58
-
59
-|c000 @FRAME
60
-|d000 @ERROR 
61
-
62
-|FF00 ;Console { pad 8 char 1 byte 1 short 2 }
63
-|FF10 ;Screen { width 2 height 2 pad 4 x 2 y 2 color 1 }
64
-|FF20 ;Sprite { pad 8 x 2 y 2 addr 2 color 1 }
65
-|FF30 ;Controller { buttons 1 }
66
-|FF40 ;Keys { key 1 }
67
-|FF50 ;Mouse { x 2 y 2 state 1 chord 1 change 1 }
68
-|FF60 ;File { pad 8 name 2 length 2 load 2 save 2 }
69
-
70
-|FFF0 .RESET .FRAME .ERROR ( vectors )
71
-|FFF8 [ 13fd 1ef3 1bf2 ] ( palette )
72 0
\ No newline at end of file
73 1
new file mode 100644
... ...
@@ -0,0 +1,9 @@
1
+|0100 ;Console { pad 8 char 1 byte 1 short 2 }
2
+|01F0 .RESET .FRAME .ERROR ( vectors )
3
+
4
+@RESET 
5
+
6
+BRK
7
+
8
+@FRAME BRK
9
+@ERROR BRK
0 10
deleted file mode 100644
... ...
@@ -1,59 +0,0 @@
1
-( tests/jump )
2
-
3
-|0100 @RESET 
4
-	
5
-	,test1 JSR2
6
-	,test2 JSR2
7
-
8
-BRK
9
-
10
-@test1
11
-	
12
-	( should print 11, 22, 33, 44 )
13
-
14
-	#11 =Console.byte
15
-	#03 JMP BRK BRK BRK 
16
-
17
-	( skip foward with id )
18
-
19
-	#22 =Console.byte
20
-	^jump JMP BRK BRK BRK @jump
21
-
22
-	( skip patterns )
23
-
24
-	#33 =Console.byte
25
-
26
-	,skip1 #12 #34 LTH JMP2?
27
-		#ff =Console.byte
28
-	@skip1
29
-
30
-	#12 #34 LTH ^skip2 #04 SUB MUL JMP
31
-		#ff =Console.byte
32
-	@skip2
33
-
34
-	#44 =Console.byte
35
-
36
-RTN
37
-
38
-@test2
39
-
40
-	,end JMP2
41
-
42
-	( should print aa, bb, cc, dd )
43
-
44
-	@label1 #aa =Console.byte ^label3 JMP
45
-	@label2 #cc =Console.byte ^label4 JMP
46
-	@label3 #bb =Console.byte ^label2 JMP
47
-	@label4 #dd =Console.byte BRK
48
-
49
-	@end
50
-
51
-RTN
52
-
53
-|c000 @FRAME
54
-|d000 @ERROR 
55
-
56
-|FF00 ;Console { pad 8 char 1 byte 1 short 2 }
57
-
58
-|FFF0 .RESET .FRAME .ERROR ( vectors )
59
-|FFF8 [ 13fd 1ef3 1bf2 ] ( palette )
60 0
\ No newline at end of file
61 1
deleted file mode 100644
... ...
@@ -1,21 +0,0 @@
1
-( tests/cond )
2
-
3
-;a { byte 1 }
4
-;b { byte1 1 byte2 1 }
5
-;c { byte1 1 byte2 1 byte3 1 }
6
-
7
-|0100 @RESET 
8
-	
9
-	#12 #00 POK
10
-
11
-	#00 PEK
12
-
13
-BRK
14
-
15
-|c000 @FRAME
16
-|d000 @ERROR 
17
-
18
-|FF00 ;Console { pad 8 char 1 byte 1 short 2 }
19
-
20
-|FFF0 .RESET .FRAME .ERROR ( vectors )
21
-|FFF8 [ 13fd 1ef3 1bf2 ] ( palette )
22 0
\ No newline at end of file
23 1
deleted file mode 100755
... ...
@@ -1,13 +0,0 @@
1
-#!/bin/bash
2
-set -e
3
-EMULATOR=./bin/emulator
4
-if [ "${1}" = '--no-sdl' ]; then
5
-    EMULATOR=./bin/emulator-nosdl
6
-    shift
7
-fi
8
-if [ -z "${1}" ]; then
9
-    printf 'usage: %s [--no-sdl] USM_FILE\n' "${0}" >&2
10
-    exit 2
11
-fi
12
-./bin/assembler "${1}" bin/boot.rom
13
-"${EMULATOR}" bin/boot.rom
14 0
similarity index 100%
15 1
rename from assembler.c
16 2
rename to src/assembler.c
17 3
similarity index 53%
18 4
rename from emulator-cli.c
19 5
rename to src/backup-debugdev.c
... ...
@@ -1,68 +1,3 @@
1
-#include <stdio.h>
2
-
3
-/*
4
-Copyright (c) 2021 Devine Lu Linvega
5
-
6
-Permission to use, copy, modify, and distribute this software for any
7
-purpose with or without fee is hereby granted, provided that the above
8
-copyright notice and this permission notice appear in all copies.
9
-
10
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
-WITH REGARD TO THIS SOFTWARE.
12
-*/
13
-
14
-#include "uxn.h"
15
-
16
-#pragma mark - Core
17
-
18
-int
19
-error(char *msg, const char *err)
20
-{
21
-	printf("Error %s: %s\n", msg, err);
22
-	return 0;
23
-}
24
-
25
-#pragma mark - Devices
26
-
27
-Uint8
28
-console_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
29
-{
30
-	Uint8 *m = u->ram.dat;
31
-	switch(b0) {
32
-	case 0x08: printf("%c", b1); break;
33
-	case 0x09: printf("0x%02x\n", b1); break;
34
-	case 0x0b: printf("0x%04x\n", (m[ptr + 0x0a] << 8) + b1); break;
35
-	}
36
-	fflush(stdout);
37
-	(void)m;
38
-	(void)ptr;
39
-	(void)b0;
40
-	return b1;
41
-}
42
-
43
-Uint8
44
-file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
45
-{
46
-	Uint8 *m = u->ram.dat;
47
-	char *name = (char *)&m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]];
48
-	Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3];
49
-	if(b0 == 0x0d) {
50
-		Uint16 addr = (m[ptr + 8 + 4] << 8) + b1;
51
-		FILE *f = fopen(name, "r");
52
-		if(f && fread(&m[addr], length, 1, f)) {
53
-			fclose(f);
54
-			printf("Loaded %d bytes, at %04x from %s\n", length, addr, name);
55
-		}
56
-	} else if(b0 == 0x0f) {
57
-		Uint16 addr = (m[ptr + 8 + 6] << 8) + b1;
58
-		FILE *f = fopen(name, "w");
59
-		if(fwrite(&m[addr], length, 1, f)) {
60
-			fclose(f);
61
-			printf("Saved %d bytes, at %04x from %s\n", length, addr, name);
62
-		}
63
-	}
64
-	return b1;
65
-}
66 1
 
67 2
 static void
68 3
 stack_diff(Stack *old, Stack *new, char *title)
... ...
@@ -181,56 +116,4 @@ debug_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
181 116
 	}
182 117
 	fflush(stdout);
183 118
 	return b1;
184
-}
185
-
186
-Uint8
187
-ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
188
-{
189
-	(void)u;
190
-	(void)ptr;
191
-	(void)b0;
192
-	return b1;
193
-}
194
-
195
-#pragma mark - Generics
196
-
197
-int
198
-start(Uxn *u)
199
-{
200
-	evaluxn(u, u->vreset);
201
-	evaluxn(u, u->vframe);
202
-}
203
-
204
-int
205
-main(int argc, char **argv)
206
-{
207
-	Uxn u;
208
-
209
-	if(argc < 2)
210
-		return error("Input", "Missing");
211
-	if(!bootuxn(&u))
212
-		return error("Boot", "Failed");
213
-	if(!loaduxn(&u, argv[1]))
214
-		return error("Load", "Failed");
215
-	if(!init())
216
-		return error("Init", "Failed");
217
-
218
-	portuxn(&u, "console", console_poke);
219
-	portuxn(&u, "empty", ppnil);
220
-	portuxn(&u, "empty", ppnil);
221
-	portuxn(&u, "empty", ppnil);
222
-	portuxn(&u, "empty", ppnil);
223
-	portuxn(&u, "empty", ppnil);
224
-	portuxn(&u, "file", file_poke);
225
-	portuxn(&u, "empty", ppnil);
226
-	portuxn(&u, "empty", ppnil);
227
-	portuxn(&u, "empty", ppnil);
228
-	portuxn(&u, "empty", ppnil);
229
-	portuxn(&u, "empty", ppnil);
230
-	portuxn(&u, "empty", ppnil);
231
-	portuxn(&u, "empty", ppnil);
232
-	portuxn(&u, "debug", debug_poke);
233
-	portuxn(&u, "system", system_poke);
234
-
235
-	return 0;
236
-}
119
+}
237 120
\ No newline at end of file
238 121
new file mode 100644
... ...
@@ -0,0 +1,112 @@
1
+#include <stdio.h>
2
+
3
+/*
4
+Copyright (c) 2021 Devine Lu Linvega
5
+
6
+Permission to use, copy, modify, and distribute this software for any
7
+purpose with or without fee is hereby granted, provided that the above
8
+copyright notice and this permission notice appear in all copies.
9
+
10
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+WITH REGARD TO THIS SOFTWARE.
12
+*/
13
+
14
+#include "uxn.h"
15
+
16
+#pragma mark - Core
17
+
18
+int
19
+error(char *msg, const char *err)
20
+{
21
+	printf("Error %s: %s\n", msg, err);
22
+	return 0;
23
+}
24
+
25
+#pragma mark - Devices
26
+
27
+Uint8
28
+console_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
29
+{
30
+	Uint8 *m = u->ram.dat;
31
+	switch(b0) {
32
+	case 0x08: printf("%c", b1); break;
33
+	case 0x09: printf("0x%02x\n", b1); break;
34
+	case 0x0b: printf("0x%04x\n", (m[ptr + 0x0a] << 8) + b1); break;
35
+	}
36
+	fflush(stdout);
37
+	(void)m;
38
+	(void)ptr;
39
+	(void)b0;
40
+	return b1;
41
+}
42
+
43
+Uint8
44
+file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
45
+{
46
+	Uint8 *m = u->ram.dat;
47
+	char *name = (char *)&m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]];
48
+	Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3];
49
+	if(b0 == 0x0d) {
50
+		Uint16 addr = (m[ptr + 8 + 4] << 8) + b1;
51
+		FILE *f = fopen(name, "r");
52
+		if(f && fread(&m[addr], length, 1, f)) {
53
+			fclose(f);
54
+			printf("Loaded %d bytes, at %04x from %s\n", length, addr, name);
55
+		}
56
+	} else if(b0 == 0x0f) {
57
+		Uint16 addr = (m[ptr + 8 + 6] << 8) + b1;
58
+		FILE *f = fopen(name, "w");
59
+		if(fwrite(&m[addr], length, 1, f)) {
60
+			fclose(f);
61
+			printf("Saved %d bytes, at %04x from %s\n", length, addr, name);
62
+		}
63
+	}
64
+	return b1;
65
+}
66
+
67
+Uint8
68
+ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
69
+{
70
+	(void)u;
71
+	(void)ptr;
72
+	(void)b0;
73
+	return b1;
74
+}
75
+
76
+#pragma mark - Generics
77
+
78
+int
79
+start(Uxn *u)
80
+{
81
+	printf("RESET --------\n");
82
+	if(!evaluxn(u, u->vreset))
83
+		return error("Reset", "Failed");
84
+	printf("FRAME --------\n");
85
+	if(!evaluxn(u, u->vframe))
86
+		return error("Frame", "Failed");
87
+	return 1;
88
+}
89
+
90
+int
91
+main(int argc, char **argv)
92
+{
93
+	Uxn u;
94
+
95
+	if(argc < 2)
96
+		return error("Input", "Missing");
97
+	if(!bootuxn(&u))
98
+		return error("Boot", "Failed");
99
+	if(!loaduxn(&u, argv[1]))
100
+		return error("Load", "Failed");
101
+
102
+	portuxn(&u, "console", console_poke);
103
+	portuxn(&u, "empty", ppnil);
104
+	portuxn(&u, "empty", ppnil);
105
+	portuxn(&u, "empty", ppnil);
106
+	portuxn(&u, "empty", ppnil);
107
+	portuxn(&u, "empty", ppnil);
108
+	portuxn(&u, "file", file_poke);
109
+	start(&u);
110
+
111
+	return 0;
112
+}
0 113
similarity index 100%
1 114
rename from emulator.c
2 115
rename to src/emulator.c
3 116
similarity index 99%
4 117
rename from uxn.c
5 118
rename to src/uxn.c
... ...
@@ -201,7 +201,6 @@ bootuxn(Uxn *u)
201 201
 	char *cptr = (char *)u;
202 202
 	for(i = 0; i < sizeof(*u); i++)
203 203
 		cptr[i] = 0;
204
-
205 204
 	return 1;
206 205
 }
207 206
 
208 207
similarity index 100%
209 208
rename from uxn.h
210 209
rename to src/uxn.h