/
bigint.pt
321 lines (274 loc) · 11.9 KB
/
bigint.pt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from testmc.mos65 import *
import pytest
from itertools import chain, count
test_rig = '''
cpu 6502
org $1000
include src/mos65/std.a65
include src/mos65/bigint.a65
'''
####################################################################
# Tests: readhex
# Buffers used for testing deliberately cross page boundaries.
INBUF = 0x6FFE
OUTBUF = 0x71FE
@pytest.mark.parametrize('input', [
(b"/"),
(b":"),
(b"@"),
])
def test_bi_readhex_error(m, R, S, input):
print('readhex_error:', input)
m.deposit(INBUF, input)
m.depword(S.buf0ptr, INBUF)
m.depword(S.buf1ptr, OUTBUF)
m.deposit(OUTBUF, [222]*5) # sentinel
with pytest.raises(m.Abort):
m.call(S.bi_readhex, R(a=len(input)))
# Length is invalid, whatever it is.
assert 222 == m.byte(OUTBUF+1) # nothing further written
@pytest.mark.parametrize('input, output', [
(b'5', b'\x05'),
(b'67', b'\x67'),
(b'89A', b'\x08\x9A'),
(b'fedc', b'\xFE\xDC'),
(b'fedcb', b'\x0F\xED\xCB'),
(b'80000', b'\x08\x00\x00'),
(b'0', b'\x00'),
(b'00000000', b'\x00'),
(b'087', b'\x87'),
(b'00000087', b'\x87'),
])
def test_bi_readhex(m, R, S, input, output):
print('bi_readhex:', input, type(input), output)
m.deposit(INBUF, input)
m.depword(S.buf0ptr, INBUF)
m.depword(S.buf1ptr, OUTBUF)
size = len(output) + 2 # length byte + value + guard byte
m.deposit(OUTBUF, [222] * size) # 222 ensures any 0s really were written
m.call(S.bi_readhex, R(a=len(input)))
bvalue = m.bytes(OUTBUF+1, len(output))
assert (len(output), output, 222,) \
== (m.byte(OUTBUF), bvalue, m.byte(OUTBUF+size-1))
####################################################################
# Tests: bi_read_dec
# Memory locations of buffers used in tests.
# These deliberately cross page boundaries for non-trivial buffer sizes.
TIN_ADDR = 0x6FFE
TOUT_ADDR = 0x71FE
TSCR_ADDR = 0x73FE
TTMP_ADDR = 0x74FE
def bi_x10_testid(val):
' Add clarity to test names and avoid very long ones. '
if type(val) is int:
return 'U' if val == 0 else 'S' # unsigned == 0
if type(val) is bytes:
h = val.hex()
return h if len(h) < 16 else h[0:16] + '...'
@pytest.mark.parametrize('signed, value', [
( 0, b'\x03'), (-1, b'\xFD'),
(-1, b'\xF4'), # min 1 byte input
(-1, b'\x0C'), # max 1 byte input
( 0, b'\x19'), # max 1 byte input (unsigned)
(-1, b'\xFF\xF3'),
(-1, b'\xF3\x34'), # min 2 byte input
(-1, b'\x0C\xCC'), # max 2 byte input
( 0, b'\x19\x99'), # max 2 byte input (unsigned)
(-1, b'\xFF\x87\x65\x43\x21'), # -20 million and a bit
(-1, b'\x02\xab\xcd\xef\x10'), # 11 billion or so
(-1, b'\xF4' + bytes(254)), # max length, approaching min value
(-1, b'\x0B' + bytes([0xFF]*254)), # max length, approaching max value
( 0, b'\x18' + bytes([0xFF]*254)), # max length, (unsigned)
], ids=bi_x10_testid)
def test_bi_x10(m, R, S, signed, value):
decval = int.from_bytes(value, byteorder='big', signed=signed)
decval10 = decval * 10
print('bi_x10: {} = {}'.format(list(map(hex, value)), decval))
expected = decval10.to_bytes(len(value), byteorder='big', signed=signed)
print(' -> {} = {}'.format(list(map(hex, expected)), decval10))
buflen = len(value)
# Guard bytes around scratch buffer show we're not writing outside it.
m.depword(TSCR_ADDR-1, 0xD3)
m.depword(TSCR_ADDR+buflen, 0xD4)
m.depword(S.bufSptr, TSCR_ADDR-1)
m.deposit(TOUT_ADDR-1, 0xDD, value, 0xDE) # guard bytes
m.depword(S.buf0ptr, TOUT_ADDR-1)
m.deposit(S.buf0len, buflen)
m.call(S.bi_x10, R(C=1))
assert b'\xD3' == m.bytes(TSCR_ADDR-1, 1)
assert b'\xD4' == m.bytes(TSCR_ADDR+buflen, 1)
assert b'\xDD' + expected + b'\xDE' == m.bytes(TOUT_ADDR-1, len(value)+2)
# Turn this on to get a sense of how fast/slow this is.
#assert not m.mpu.processorCycles
@pytest.mark.parametrize('sign, input, buf', [
( 0, b'0', [0x00]),
( 0, b'255', [0xFF]),
(-1, b'1', [0xFF]),
(-1, b'128', [0x80]),
(-1, b'129', [0x7F]),
( 0, b'0', [0x00, 0x00]),
( 0, b'0', [0x00, 0x00]),
(-1, b'0', [0x00, 0x00]),
( 0, b'0000', [0x00, 0x00]),
( 0, b'9', [0x00, 0x09]),
(-1, b'3', [0xFF, 0xFD]),
( 0, b'10', [0x00, 0x0A]),
# Some smaller byte-boundry edge cases.
# These help test carry propagation.
( 0, b'32767', [ 0x7F, 0xFF]),
( 0, b'32768', [ 0x80, 0x00]),
( 0, b'2147483647', [ 0x7F, 0xFF, 0xFF, 0xFF]),
( 0, b'2147483648', [ 0x80, 0x00, 0x00, 0x00]),
(-1, b'32768', [ 0x80, 0x00]),
(-1, b'32769', [ 0x7F, 0xFF]),
(-1, b'32769', [ 0xFF, 0x7F, 0xFF]),
(-1, b'2147483648', [ 0x80, 0x00, 0x00, 0x00]),
(-1, b'2147483648', [0xFF, 0x80, 0x00, 0x00, 0x00]),
(-1, b'2147483649', [ 0x7F, 0xFF, 0xFF, 0xFF]),
(-1, b'2147483649', [0xFF, 0x7F, 0xFF, 0xFF, 0xFF]),
( 0, b'65432', [ 0xFF, 0x98]),
( 0, b'65432', [ 0x00, 0xFF, 0x98]),
( 0, b'65432', [ 0x00, 0x00, 0xFF, 0x98]),
(-1, b'64321', [ 0xFF, 0x04, 0xBF]),
(-1, b'64321', [ 0xFF, 0xFF, 0xFF, 0x04, 0xBF]),
( 0, b'78912345678901', [ 0x47, 0xC5, 0x36, 0x55, 0x24, 0x35]),
(-1, b'78912345678901', [ 0xB8, 0x3A, 0xC9, 0xAA, 0xDB, 0xCB]),
(-1, b'78912345678901', [0xFF, 0xB8, 0x3A, 0xC9, 0xAA, 0xDB, 0xCB]),
])
def test_bi_read_decdigits(m, R, S, sign, input, buf):
print('bi_read_decdigits:', input, buf)
if sign != 0: sign = 0xFF
m.deposit(S.sign, sign)
buf = bytes(buf)
osize = len(buf)
m.deposit(S.buf0len, osize)
m.deposit(TOUT_ADDR-1, 0xF0, [0xF1]*osize, 0xF2)
m.depword(S.buf0ptr, TOUT_ADDR-1)
m.deposit(TIN_ADDR-1, 0xDD, input, 0xDF) # guard bytes
m.depword(S.buf1ptr, TIN_ADDR)
m.deposit(S.buf1len, len(input))
m.deposit(TSCR_ADDR-1, 0xFA) # guard bytes
m.deposit(TSCR_ADDR+osize, 0xFB)
m.depword(S.bufSptr, TSCR_ADDR-1)
# This value should be moderately different for many tests.
xpreserved = (sum(map(int, input)) + sign) % 191 + 31
# XXX add `sign` param for positive/negative
try:
m.call(S.bi_read_decdigits, R(x=xpreserved))
finally:
print(m.regs)
print('di={} buf0={}'.format(
m.byte(S.curdigit), m.hexdump(TOUT_ADDR-1, osize+2)))
assert b'\xF0' + buf + b'\xF2' == m.bytes(TOUT_ADDR-1, osize+2)
assert b'\xFA' == m.bytes(TSCR_ADDR-1, 1)
assert b'\xFB' == m.bytes(TSCR_ADDR+osize, 1)
assert R(x=xpreserved) == m.regs
@pytest.mark.skip(msg='These tests can take up to 10 seconds each to run')
@pytest.mark.parametrize('sign', [
0x00, # 3,531,433 cyles, about 3.5 seconds on 1 MHz 6502
0xFF, # 3,791,023 cyles, almost 4 seconds on 1 MHz 6502
])
def test_bi_read_decdigits_max(m, S, sign):
m.deposit(S.sign, sign)
input = b'9'*255
input_i = int(input)
if sign: input_i = -input_i
expected = list(input_i.to_bytes(128, byteorder='big', signed=True))
print(input_i, '\n', expected)
m.deposit(TIN_ADDR-1, 122, list(input), 133) # guard bytes
m.depword(S.buf1ptr, TIN_ADDR)
m.deposit(S.buf1len, len(input))
osize = len(expected)
m.deposit(S.buf0len, osize)
m.deposit(TOUT_ADDR-1, 155, [0]*osize, 166)
m.depword(S.buf0ptr, TOUT_ADDR-1)
m.depword(S.bufSptr, TSCR_ADDR-1)
m.call(S.bi_read_decdigits)
assert [155] + expected + [166] == list(m.bytes(TOUT_ADDR-1, osize+2))
# Uncommit to show number of cycles taken.
#assert not m.mpu.processorCycles
@pytest.mark.parametrize('input, output', [
# Zero uses special-case code
(b'0', [0x00]),
(b'+0', [0x00]),
(b'-0', [0x00]),
# Single digits
(b'1', [0x01]),
(b'-1', [0xFF]),
(b'9', [0x09]),
(b'-9', [0xF7]),
# Stripping of leading zeros
(b'0000', [ 0x00]),
(b'+0001', [ 0x01]),
(b'-0001', [ 0xFF]),
(b'0000234', [0x00, 0xEA]),
(b'+000000234', [0x00, 0xEA]),
(b'-00000000234', [0xFF, 0x16]),
(b'-00000000345', [0xFE, 0xA7]),
# Edge cases for sign extension
(b'127', [ 0x7F]),
(b'128', [ 0x00, 0x80]),
(b'32767', [ 0x7F, 0xFF]),
(b'32768', [0x00, 0x80, 0x00]),
(b'-128', [ 0x80]),
(b'-129', [ 0xFF, 0x7F]),
(b'-32768', [ 0x80, 0x00]),
(b'-32769', [0xFF, 0x7F, 0xFF]),
# Some biggish numbers
(b'12345678', [0x00, 0xBC, 0x61, 0x4E]),
(b'-1234567', [ 0xED, 0x29, 0x79]),
(b'-12345678', [0xFF, 0x43, 0x9E, 0xB2]),
(b'999999999999', [0x00, 0xE8, 0xD4, 0xA5, 0x0F, 0xFF]),
(b'-999999999999', [0XFF, 0X17, 0X2B, 0X5A, 0XF0, 0X01]),
])
def test_bi_read_dec(m, R, S, input, output):
print('bi_read_dec:', input, output)
# Allocate temp/scratch buffers. We do this here until we have an alloc
# system that will let bi_read_dec alloc its own temp/scratch buffers.
# We probably should be allocating a correctly sized buf rather than
# 128 (big enough for all inputs) for better bounds checks.
# Note that the pointers point to 1 below the buffer itself.
scratchlen = 128
m.depword(S.bufSptr, TSCR_ADDR-1)
m.deposit(TSCR_ADDR-1, 111, [112]*scratchlen, 113)
m.depword(S.buf0ptr, TTMP_ADDR-1)
m.deposit(TTMP_ADDR-1, 121, [122]*scratchlen, 123)
m.depword(S.buf1ptr, TIN_ADDR)
m.deposit(TIN_ADDR-1, 0xD3, input, 0xD5)
m.depword(S.buf2ptr, TOUT_ADDR)
# guard, output length, output value, guard
m.deposit(TOUT_ADDR-1, 0xDD, 222, [223] * len(output), 0xE0)
try:
m.call(S.bi_read_dec, R(y=len(input)))
except m.Abort as ex:
print(ex)
assert 0
finally:
#print('buf1ptr', m.hexdump(m.word(S.buf1ptr), 8))
print('buf0ptr', m.hexdump(m.word(S.buf0ptr), 8))
print('buf0len', hex(m.byte(S.buf0len)))
#print(' buf0 TTMP-1', list(map(hex, m.bytes(TTMP_ADDR-1, 12)))) #XXX
print('buf2ptr', m.hexdump(m.word(S.buf2ptr), 8))
#print(' buf2 TOUT-1', list(map(hex, m.bytes(TOUT_ADDR-1, 12)))) #XXX
# Assert scratch buffers were not written out of bounds
assert 111 == m.byte(TSCR_ADDR-1)
assert 113 == m.byte(TSCR_ADDR+scratchlen)
assert 121 == m.byte(TTMP_ADDR-1)
assert 123 == m.byte(TTMP_ADDR+scratchlen)
# Assert input buffer is unchanged
assert b'\xD3' + input + b'\xD5' == m.bytes(TIN_ADDR-1, len(input)+2)
# Assert output buffer is correct and without out-of-bounds writes.
assert b'\xDD' + bytes([len(output)]) + bytes(output) + b'\xE0' \
== m.bytes(TOUT_ADDR-1, len(output)+3)
@pytest.mark.parametrize('bufstart', (0x70FD, 0x70FE, 0x70FF, 0x7100))
def test_bi_read_dec_bufstart(m, R, S, bufstart):
' Edge case where the sign char is on the last byte of a page, etc. '
scratch, temp, inbuf, outbuf \
= bufstart, bufstart+0x100, bufstart+0x200, bufstart+0x300
input = b'+003'
m.depword(S.bufSptr, scratch)
m.depword(S.buf0ptr, temp)
m.depword(S.buf1ptr, inbuf) ; m.deposit(inbuf, input)
m.depword(S.buf2ptr, outbuf)
m.call(S.bi_read_dec, R(y=len(input)))
assert b'\x00\x01\x03\x00' == m.bytes(outbuf-1, 4)