Browse code

Moved printing routines from tests/opcodes to console example

Andrew Alderwick authored on 24/04/2021 08:30:36
Showing 2 changed files
1 1
deleted file mode 100644
... ...
@@ -1,163 +0,0 @@
1
-(
2
-	tests/opcodes : automated testing of opcodes
3
-
4
-	This file generates a lot of stack underflows on purpose:
5
-	it's handy to supress all the warning by piping through grep
6
-
7
-	| grep -vF 'Halted: Working-stack underflow'
8
-)
9
-
10
-;test { code 2 label 2 status 1 }
11
-;counts { failed 2 passed 2 unknown 2 }
12
-;number { started 1 }
13
-
14
-|0110 ;Console { vector 2 pad 6 char 1 byte 1 short 2 string 2 }
15
-|01F0 .RESET .FRAME .ERROR ( vectors )
16
-
17
-%PASS? { ,result JMP2 BRK2r LITr EOR2 DUP }
18
-%PASS { #01 PASS? }
19
-%FAIL { #00 PASS? }
20
-
21
-|0200
22
-
23
-@tests
24
-	ADD FAIL [ add-needs-two 00 ]
25
-	#01 ADD FAIL [ add-needs-two 00 ]
26
-	#01 #02 ADD #03 EQU PASS? [ add-result 00 ]
27
-	LITr [ fe ] STHr #fe EQU PASS? [ litr 00 ]
28
-	LIT2r [ fe dc ] STH2r #fedc EQU2 PASS? [ lit2r 00 ]
29
-	#01 #02 ADD #ff EQU PASS? [ this-test-fails 00 ]
30
-
31
-	,finish JMP2
32
-
33
-@RESET
34
-	,tests =test.code
35
-	,strings-start ,print-string JSR2
36
-	BRK
37
-	
38
-@ERROR BRK
39
-
40
-@FRAME
41
-	~test.status ,recover JNZ2
42
-	#01 =test.status
43
-	~test.code
44
-	DUP2 ,find-label JSR2
45
-		DUP2 =test.label
46
-		,find-code JSR2 =test.code
47
-	JMP2
48
-
49
-@find-label ( ptr₂ -- following-label-ptr₂ )
50
-	DUP2            PEK2 LIT BRK2r NEQ ^$next JNZ
51
-	DUP2 #0001 ADD2 PEK2 LIT LITr  NEQ ^$next JNZ
52
-	DUP2 #0002 ADD2 PEK2 LIT EOR2  NEQ ^$next JNZ
53
-	DUP2 #0003 ADD2 PEK2 LIT DUP   NEQ ^$next JNZ
54
-	#0004 ADD2 JMP2r
55
-
56
-	$next
57
-	#0001 ADD2 ^find-label JMP
58
-
59
-@find-code ( label-ptr₂ -- following-code-ptr₂ )
60
-	DUP2 PEK2
61
-	,$not-end JNZ2
62
-
63
-	$end
64
-	#0001 ADD2
65
-	JMP2r
66
-
67
-	$not-end
68
-	#0001 ADD2 ^find-code JMP
69
-
70
-@recover
71
-	( would it have been a PASS or FAIL? )
72
-	~test.label #000a SUB2 PEK2 LIT LIT EQU ,$clear JNZ2
73
-	#02 ^result JMP
74
-
75
-	$clear
76
-	( I would have executed a PASS or FAIL, so invert the result )
77
-	~test.label #0009 SUB2 PEK2 #00 EQU ^result JMP
78
-
79
-@result
80
-	DUP #02 MUL #00 SWP ,counts ADD2
81
-		DUP2 LDR2 #0001 ADD2 SWP2 STR2
82
-	#00 =test.status
83
-	,strings-test ^print-string JSR
84
-	#00 SWP ,strings-pass ,strings-fail SUB2 MUL2 ,strings-fail ADD2 ^print-string JSR
85
-	,strings-colon ^print-string JSR
86
-	~test.label ^print-string JSR
87
-	#0a =Console.char
88
-	POP #fc JMP
89
-	BRK
90
-
91
-@finish
92
-	,strings-finish ^print-string JSR
93
-	~counts.passed ^print-decimal JSR
94
-	,strings-passed ^print-string JSR
95
-	~counts.failed ^print-decimal JSR
96
-	,strings-failed ^print-string JSR
97
-	~counts.unknown ^print-decimal JSR
98
-	,strings-unknown ^print-string JSR
99
-	
100
-	( stop executing tests )
101
-	LIT BRK ,FRAME POK2
102
-	BRK
103
-
104
-@print-string ( string₂ -- )
105
-	DUP2 PEK2 DUP
106
-	,$not-end JNZ2
107
-
108
-	$end
109
-	POP POP2 JMP2r
110
-
111
-	$not-end
112
-	DUP LIT BRK2r EQU ,$end JNZ2
113
-	=Console.char
114
-	#0001 ADD2 ^print-string JMP
115
-
116
-@print-decimal ( short₂ -- )
117
-	#00 =number.started
118
-	DUP2 #2710 DIV2 DUP2 ^$digit JSR #2710 MUL2 SUB2
119
-	DUP2 #03e8 DIV2 DUP2 ^$digit JSR #03e8 MUL2 SUB2
120
-	DUP2 #0064 DIV2 DUP2 ^$digit JSR #0064 MUL2 SUB2
121
-	DUP2 #000a DIV2 DUP2 ^$digit JSR #000a MUL2 SUB2
122
-	^$digit JSR
123
-	~number.started #00 EQU JMP JMP2r
124
-	#30 =Console.char
125
-	JMP2r
126
-
127
-	$digit
128
-	SWP POP
129
-	DUP ~number.started ORA #02 JNZ
130
-	POP JMP2r
131
-	#30 ADD =Console.char
132
-	#01 =number.started
133
-	JMP2r
134
-
135
-@print-short ( short₂ -- )
136
-	#30 =Console.char
137
-	#78 =Console.char
138
-	DUP2 #000c SFT2 ^$digit JSR
139
-	DUP2 #0008 SFT2 ^$digit JSR
140
-	DUP2 #0004 SFT2 ^$digit JSR
141
-	                ^$digit JSR
142
-	JMP2r
143
-
144
-	$digit
145
-	#0f AND DUP #0a LTH #03 JNZ
146
-		#27 ADD
147
-	#30 ADD =Console.char
148
-	POP
149
-	JMP2r
150
-
151
-@strings
152
-	$start [ 0a Testing 20 started. 0a 0a 00 ]
153
-	$test [ Test 20 00 ]
154
-	$fail [ FAIL 00 ]
155
-	$pass [ pass 00 ]
156
-	      [ UNKNOWN 00 ]
157
-	$at [ at 20 00 ]
158
-	$colon [ : 20 00 ]
159
-	$finish [ 0a Testing 20 complete. 0a 00 ]
160
-	$passed [ 20 passed, 20 00 ]
161
-	$failed [ 20 failed, 20 00 ]
162
-	$unknown [ 20 were 20 unknown. 0a 00 ]
163
-
... ...
@@ -6,11 +6,20 @@
6 6
 
7 7
 |10 @Console    [ &pad $8 &char $1 ]
8 8
 
9
+( variables )
10
+
11
+|0000
12
+
13
+@number [ &started $1 ]
14
+
9 15
 ( init )
10 16
 
11 17
 |0100 ( -> )
12 18
 	
13 19
 	;hello-word ;print JSR2
20
+	#ffff ;print-hexadecimal JSR2
21
+	;is-word ;print JSR2
22
+	#ffff ;print-decimal JSR2
14 23
 	
15 24
 BRK
16 25
 
... ...
@@ -19,9 +28,48 @@ BRK
19 28
 	&loop
20 29
 		( send ) DUP2 GET .Console/char DEO
21 30
 		( incr ) #0001 ADD2
22
-		( loop ) DUP2 GET #00 NEQ ,&loop JNZ
31
+		( loop ) DUP2 GET ,&loop JNZ
23 32
 	POP2
24 33
 
25 34
 RTN
26 35
 
27
-@hello-word "hello 20 "World!
36
+@print-hexadecimal ( short -- )
37
+	LIT '0 .Console/char DEO
38
+	LIT 'x .Console/char DEO
39
+	DUP2 #000c SFT2 ,&digit JSR
40
+	DUP2 #0008 SFT2 ,&digit JSR
41
+	DUP2 #0004 SFT2 ,&digit JSR
42
+	                ,&digit JSR
43
+RTN
44
+
45
+	&digit
46
+	#0f AND DUP #0a LTH ,&not-alpha JNZ
47
+		#27 ADD
48
+	&not-alpha
49
+	LIT '0 ADD .Console/char DEO
50
+	POP
51
+RTN
52
+
53
+@print-decimal ( short -- )
54
+	#00 .number/started POK
55
+	DUP2 #2710 DIV2 DUP2 ,&digit JSR #2710 MUL2 SUB2
56
+	DUP2 #03e8 DIV2 DUP2 ,&digit JSR #03e8 MUL2 SUB2
57
+	DUP2 #0064 DIV2 DUP2 ,&digit JSR #0064 MUL2 SUB2
58
+	DUP2 #000a DIV2 DUP2 ,&digit JSR #000a MUL2 SUB2
59
+	                     ,&digit JSR
60
+	.number/started PEK ,&end JNZ
61
+	LIT '0 .Console/char DEO
62
+	&end
63
+RTN
64
+
65
+	&digit
66
+	SWP POP
67
+	DUP .number/started PEK ORA #02 JNZ
68
+	POP JMP2r
69
+	LIT '0 ADD .Console/char DEO
70
+	#01 .number/started POK
71
+RTN
72
+
73
+@hello-word "hello 20 "World! 0a 00
74
+@is-word 20 "is 20 00
75
+