1
0
mirror of https://github.com/mnaberez/py65.git synced 2025-03-05 09:30:23 +00:00

Move BCD lookup into module, added some unit tests for SBC.

This commit is contained in:
Mike Naberezny 2008-08-17 21:04:09 +00:00
parent 0d441afb54
commit db55b300fe
3 changed files with 111 additions and 50 deletions

38
bcd_conversion.py Normal file
View File

@ -0,0 +1,38 @@
def convert_to_bin(bcd):
return bcd2bin[bcd]
def convert_to_bcd(bin):
return bin2bcd[bin]
bcd2bin = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, # 0x00
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, # 0x10
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, # 0x20
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, # 0x30
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, # 0x40
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, # 0x50
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, # 0x60
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, # 0x70
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, # 0x80
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105, # 0x90
100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115, # 0xA0
110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, # 0xB0
120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135, # 0xC0
130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145, # 0xD0
140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155, # 0xE0
150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165 # 0xF0
]
bin2bcd = [
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99
]

68
mpu.py
View File

@ -1,3 +1,5 @@
from bcd_conversion import convert_to_bin, convert_to_bcd
class MPU:
# vectors
ResetTo = 0xfffc
@ -249,7 +251,7 @@ class MPU:
tmp = 1
else:
tmp = 0
data = self.bcd2dec[data]+self.bcd2dec[self.a]+tmp
data = convert_to_bin(data) + convert_to_bin(self.a) + tmp
self.flags &= ~(self.CARRY+self.OVERFLOW+self.NEGATIVE+self.ZERO)
if data > 99:
self.flags |= self.CARRY + self.OVERFLOW
@ -259,7 +261,7 @@ class MPU:
self.flags |= self.ZERO
else:
self.flags |= data & 128
self.a = self.dec2bcd[data]
self.a = convert_to_bcd(data)
else:
if self.flags & self.CARRY:
tmp = 1
@ -335,11 +337,11 @@ class MPU:
data = self.ByteAt(x())
if self.flags & self.DECIMAL:
if self.flags & self.CARRY:
tmp = 0
borrow = 0
else:
tmp = 1
borrow = 1
data = self.bcd2dec[a] - self.bcd2dec[data] - tmp
data = convert_to_bin(self.a) - convert_to_bin(data) - borrow
self.flags &= ~(self.CARRY + self.ZERO + self.NEGATIVE + self.OVERFLOW)
if data == 0:
self.flags |= self.ZERO + self.CARRY
@ -348,14 +350,14 @@ class MPU:
else:
self.flags |= self.NEGATIVE
data +=100
self.a = self.dec2bcd[data]
self.a = convert_to_bcd(data)
else:
if self.flags & self.CARRY:
tmp = 0
borrow = 0
else:
tmp = 1
borrow = 1
data = self.a - data - tmp
data = self.a - data - borrow
self.flags &= ~(self.CARRY + self.ZERO + self.OVERFLOW + self.NEGATIVE)
if data == 0:
self.flags |= self.ZERO + self.CARRY
@ -363,8 +365,8 @@ class MPU:
self.flags |= self.CARRY
else:
self.flags |= self.OVERFLOW
self.flags |= data & 128
self.a = data & 255
self.flags |= data & self.NEGATIVE
self.a = data & 0xFF
def opDECR(self, x):
addr = x()
@ -669,7 +671,7 @@ class MPU:
tmp = 0
if self.flags & self.DECIMAL:
data = self.bcd2dec[data] + self.bcd2dec[a] + tmp
data = convert_to_bin(data) + convert_to_bin(self.a) + tmp
self.flags &= ~(self.CARRY+self.OVERFLOW+self.NEGATIVE+self.ZERO)
if data > 99:
self.flags |= self.CARRY+self.OVERFLOW
@ -678,7 +680,7 @@ class MPU:
self.flags |= self.ZERO
else:
self.flags |= self.data & 128
self.a = self.dec2bcd[data]
self.a = convert_to_bcd(data)
else:
if self.flags & self.CARRY:
tmp = 1
@ -1044,7 +1046,7 @@ class MPU:
tmp = 0
else:
tmp = 1
data = self.bcd2dec[a] - self.bcd2dec[data] - tmp
data = convert_to_bin(self.a) - convert_to_bin(data) - tmp
self.flags &= ~(self.CARRY+self.ZERO+self.NEGATIVE+self.OVERFLOW)
if data == 0:
self.flags |= self.ZERO + self.CARRY
@ -1053,7 +1055,7 @@ class MPU:
else:
self.flags |= self.NEGATIVE
data +=100
self.a = self.dec2bcd[data]
self.a = convert_to_bcd(data)
else:
if self.flags & self.CARRY:
tmp = 0
@ -1179,7 +1181,7 @@ class MPU:
2, 5, 0, 0, 0, 4, 6, 0, 2, 4, 0, 0, 0, 4, 7, 0 # F0
]
extracycles= [
extracycles = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 00
2, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, # 10
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 20
@ -1197,37 +1199,3 @@ class MPU:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # E0
2, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 # F0
]
# lookup tables for opBCD math.
bcd2dec = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, # 0x00
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, # 0x10
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, # 0x20
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, # 0x30
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, # 0x40
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, # 0x50
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, # 0x60
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, # 0x70
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, # 0x80
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105, # 0x90
100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115, # 0xA0
110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, # 0xB0
120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135, # 0xC0
130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145, # 0xD0
140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155, # 0xE0
150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165 # 0xF0
]
dec2bcd= [
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99
]

View File

@ -3322,6 +3322,61 @@ class MPUTests(unittest.TestCase):
self.assertEquals(0xC004, mpu.pc)
self.assertEquals(0xFF, mpu.sp)
# SBC
def test_sbc_abs_all_zeros_and_no_borrow_is_zero(self):
mpu = MPU()
mpu.flags &= ~(mpu.DECIMAL)
mpu.flags |= mpu.CARRY # borrow = 0
mpu.a = 0x00
mpu.memory[0x0000:0x0002] = (0xED, 0xCD, 0xAB) #=> SBC $ABCD
mpu.memory[0xABCD] = 0x00
mpu.step()
self.assertEquals(0x00, mpu.a)
self.assertEquals(0, mpu.flags & mpu.NEGATIVE)
self.assertEquals(mpu.CARRY, mpu.CARRY)
self.assertEquals(mpu.ZERO, mpu.flags & mpu.ZERO)
def test_sbc_abs_downto_zero_no_borrow_sets_z_clears_n(self):
mpu = MPU()
mpu.flags &= ~(mpu.DECIMAL)
mpu.flags |= mpu.CARRY # borrow = 0
mpu.a = 0x01
mpu.memory[0x0000:0x0002] = (0xED, 0xCD, 0xAB) #=> SBC $ABCD
mpu.memory[0xABCD] = 0x01
mpu.step()
self.assertEquals(0x00, mpu.a)
self.assertEquals(0, mpu.flags & mpu.NEGATIVE)
self.assertEquals(mpu.CARRY, mpu.CARRY)
self.assertEquals(mpu.ZERO, mpu.flags & mpu.ZERO)
def test_sbc_abs_downto_zero_with_borrow_sets_z_clears_n(self):
mpu = MPU()
mpu.flags &= ~(mpu.DECIMAL)
mpu.flags &= ~(mpu.CARRY) # borrow = 1
mpu.a = 0x01
mpu.memory[0x0000:0x0002] = (0xED, 0xCD, 0xAB) #=> SBC $ABCD
mpu.memory[0xABCD] = 0x00
mpu.step()
self.assertEquals(0x00, mpu.a)
self.assertEquals(0, mpu.flags & mpu.NEGATIVE)
self.assertEquals(mpu.CARRY, mpu.CARRY)
self.assertEquals(mpu.ZERO, mpu.flags & mpu.ZERO)
def test_sbc_abs_downto_four_with_borrow_clears_z_n(self):
mpu = MPU()
mpu.flags &= ~(mpu.DECIMAL)
mpu.flags &= ~(mpu.CARRY) # borrow = 1
mpu.a = 0x07
mpu.memory[0x0000:0x0002] = (0xED, 0xCD, 0xAB) #=> SBC $ABCD
mpu.memory[0xABCD] = 0x02
mpu.step()
self.assertEquals(0x04, mpu.a)
self.assertEquals(0, mpu.flags & mpu.NEGATIVE)
self.assertEquals(0, mpu.flags & mpu.ZERO)
self.assertEquals(mpu.CARRY, mpu.CARRY)
# SEC
def test_sec_sets_carry_flag(self):