Browse code

Basic parts

neauoire authored on 29/01/2021 20:14:37
Showing 6 changed files
... ...
@@ -3,4 +3,6 @@
3 3
 *png~
4 4
 *gif~
5 5
 *bmp~
6
-uxn
7 6
\ No newline at end of file
7
+uxn
8
+uxnasm
9
+*.rom
8 10
\ No newline at end of file
... ...
@@ -48,11 +48,15 @@ VALUE       OPCODE  EXPLANATION
48 48
 
49 49
 ### PPU
50 50
 
51
+### Assembly
52
+
53
+```
54
+2 2 + $ef
55
+```
56
+
51 57
 ### Assembler
52 58
 
53 59
 
54 60
 ### Emulator
55 61
 
56 62
 - SDL Layer
57
-
58
-### Assembly
... ...
@@ -1,12 +1,15 @@
1 1
 #!/bin/bash
2 2
 
3 3
 # format code
4
+clang-format -i uxnasm.c
4 5
 clang-format -i uxn.c
5 6
 
6 7
 # remove old
8
+rm -f ./uxnasm
7 9
 rm -f ./uxn
8 10
 
9 11
 # debug(slow)
12
+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 uxnasm.c -o uxnasm
10 13
 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 -o uxn
11 14
 
12 15
 # build(fast)
... ...
@@ -15,12 +18,6 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr
15 18
 # Size
16 19
 echo "Size: $(du -sk ./uxn)"
17 20
 
18
-# Install
19
-if [ -d "$HOME/bin" ] && [ -e ./uxn ]
20
-then
21
-	cp ./uxn $HOME/bin
22
-    echo "Installed: $HOME/bin" 
23
-fi
24
-
25 21
 # run
26
-./uxn 
22
+./uxnasm program.usm program.rom
23
+# ./uxn program.rom
27 24
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+2 2 + .
0 2
\ No newline at end of file
... ...
@@ -11,28 +11,93 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 11
 WITH REGARD TO THIS SOFTWARE.
12 12
 */
13 13
 
14
-typedef unsigned char Uint8;
15 14
 #define STACK_DEPTH 256
15
+#define ECHO 1
16
+
17
+typedef unsigned char Uint8;
18
+
19
+typedef struct {
16 20
 
17
-Uint8 data[STACK_DEPTH];
21
+} Computer;
22
+
23
+Uint8 sptr;
24
+Uint8 stack[STACK_DEPTH];
18 25
 Uint8 address[STACK_DEPTH];
26
+Uint8 memory[STACK_DEPTH];
19 27
 
20 28
 void
21
-stackprint(Uint8 *stack)
29
+stackprint(Uint8 *s, Uint8 len)
22 30
 {
23 31
 	int i;
24
-	for(i = 0; i < STACK_DEPTH; ++i) {
32
+	for(i = 0; i < len; ++i) {
25 33
 		if(i % 16 == 0)
26 34
 			printf("\n");
27
-		printf("%02x ", stack[i]);
35
+		if(sptr == i)
36
+			printf("[%02x]", s[i]);
37
+		else
38
+			printf(" %02x ", s[i]);
28 39
 	}
29 40
 	printf("\n");
30 41
 }
31 42
 
43
+void
44
+op_push(Uint8 *s, Uint8 v)
45
+{
46
+	s[sptr++] = v;
47
+}
48
+
49
+void
50
+op_pop(Uint8 *s)
51
+{
52
+	s[sptr--] = 0x00;
53
+}
54
+
55
+void
56
+reset(Computer *cpu)
57
+{
58
+}
59
+
60
+int
61
+disk(Computer *cpu, FILE *f)
62
+{
63
+	int i;
64
+	unsigned short buffer[256];
65
+	reset(cpu);
66
+	if(!fread(buffer, sizeof(buffer), 1, f))
67
+		return 0;
68
+	/*
69
+	
70
+	for(i = 0; i < 128; i++) {
71
+		cpu->memory[i * 2] |= (buffer[i] >> 8) & 0xFF;
72
+		cpu->memory[i * 2 + 1] |= buffer[i] & 0xFF;
73
+	}
74
+	*/
75
+	return 1;
76
+}
77
+
78
+void
79
+run(Computer *cpu, int debug)
80
+{
81
+}
82
+
32 83
 int
33 84
 main(int argc, char *argv[])
34 85
 {
35
-	printf("hello\n");
36
-	stackprint(data);
86
+	FILE *f;
87
+	Computer cpu;
88
+	if(argc < 2)
89
+		return error("No input.");
90
+	if(!(f = fopen(argv[1], "rb")))
91
+		return error("Missing input.");
92
+	if(!disk(&cpu, f))
93
+		return error("Unreadable input.");
94
+	run(&cpu, ECHO);
95
+	/* program */
96
+	op_push(stack, 0xef);
97
+	op_pop(stack);
98
+	op_push(stack, 0x02);
99
+	op_push(stack, 0x03);
100
+	/* print result */
101
+	stackprint(stack, 0x40);
37 102
 	return 0;
38 103
 }
39 104
new file mode 100644
... ...
@@ -0,0 +1,49 @@
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
+#define BUFLEN 256
15
+
16
+typedef unsigned char Uint8;
17
+typedef unsigned char Uint16;
18
+
19
+unsigned short program[BUFLEN];
20
+
21
+void
22
+pass1(FILE *f)
23
+{
24
+	int instrid = 0;
25
+	char line[BUFLEN];
26
+	while(fgets(line, BUFLEN, f)) {
27
+		printf("%s\n", line);
28
+	}
29
+}
30
+
31
+int
32
+error(char *name)
33
+{
34
+	printf("Error: %s\n", name);
35
+	return 0;
36
+}
37
+
38
+int
39
+main(int argc, char *argv[])
40
+{
41
+	FILE *f;
42
+	if(argc < 3)
43
+		return error("No input.");
44
+	if(!(f = fopen(argv[1], "r")))
45
+		return error("Missing input.");
46
+	pass1(f);
47
+	fwrite(program, sizeof(program), 1, fopen(argv[2], "wb"));
48
+	return 0;
49
+}