1109 lines
82 KiB
Plaintext
1109 lines
82 KiB
Plaintext
* source: C:\Users\peter.russell\Documents\Personal\multiply\Contrib\psr\multiply\src\quarter-square.asm.py
|
|
36 enableListing()
|
|
37
|
|
38 # Inputs
|
|
39 a, b = zpByte(), zpByte()
|
|
40 # Output
|
|
41 result = zpByte(2)
|
|
42 # Used for storage
|
|
43 tmp = zpByte()
|
|
44 # Used for the continuation address after lookup.
|
|
45 continuation = zpByte()
|
|
46 # Used for the action to take with the high byte.
|
|
47 high_byte_action = zpByte()
|
|
48
|
|
49
|
|
50 # The following code implements a lookup table of floored quarter squares,
|
|
51 # for values up to 255.
|
|
52 # This is supposed to enable a fast multiplication for 7-bit numbers.
|
|
53
|
|
54 # First the high-bytes.
|
|
55 # The table is shifted down by 32 places, as the first 32 high-bytes are all zero
|
|
56 # This allows us to have code later in the page which we can branch back to.
|
|
57 align(0x100, size=0x100)
|
|
58 label("Quarter-squares lookup table")
|
|
59 for i in range(32, 256):
|
|
60 val = math.floor(i**2 / 4)
|
|
address
|
|
| encoding
|
|
| | instruction
|
|
| | | operands
|
|
| | | |
|
|
V V V V
|
|
Quarter-squares lookup table: 61 ld(hi(val))
|
|
0000 0001 ld $01 ;$0100 = 256 = floor(32 ** 2 / 4); $0100 >> 8 = $01
|
|
0001 0001 ld $01 ;$0110 = 272 = floor(33 ** 2 / 4); $0110 >> 8 = $01
|
|
0002 0001 ld $01 ;$0121 = 289 = floor(34 ** 2 / 4); $0121 >> 8 = $01
|
|
0003 0001 ld $01 ;$0132 = 306 = floor(35 ** 2 / 4); $0132 >> 8 = $01
|
|
0004 0001 ld $01 ;$0144 = 324 = floor(36 ** 2 / 4); $0144 >> 8 = $01
|
|
0005 0001 ld $01 ;$0156 = 342 = floor(37 ** 2 / 4); $0156 >> 8 = $01
|
|
0006 0001 ld $01 ;$0169 = 361 = floor(38 ** 2 / 4); $0169 >> 8 = $01
|
|
0007 0001 ld $01 ;$017c = 380 = floor(39 ** 2 / 4); $017c >> 8 = $01
|
|
0008 0001 ld $01 ;$0190 = 400 = floor(40 ** 2 / 4); $0190 >> 8 = $01
|
|
0009 0001 ld $01 ;$01a4 = 420 = floor(41 ** 2 / 4); $01a4 >> 8 = $01
|
|
000a 0001 ld $01 ;$01b9 = 441 = floor(42 ** 2 / 4); $01b9 >> 8 = $01
|
|
000b 0001 ld $01 ;$01ce = 462 = floor(43 ** 2 / 4); $01ce >> 8 = $01
|
|
000c 0001 ld $01 ;$01e4 = 484 = floor(44 ** 2 / 4); $01e4 >> 8 = $01
|
|
000d 0001 ld $01 ;$01fa = 506 = floor(45 ** 2 / 4); $01fa >> 8 = $01
|
|
000e 0002 ld $02 ;$0211 = 529 = floor(46 ** 2 / 4); $0211 >> 8 = $02
|
|
000f 0002 ld $02 ;$0228 = 552 = floor(47 ** 2 / 4); $0228 >> 8 = $02
|
|
0010 0002 ld $02 ;$0240 = 576 = floor(48 ** 2 / 4); $0240 >> 8 = $02
|
|
0011 0002 ld $02 ;$0258 = 600 = floor(49 ** 2 / 4); $0258 >> 8 = $02
|
|
0012 0002 ld $02 ;$0271 = 625 = floor(50 ** 2 / 4); $0271 >> 8 = $02
|
|
0013 0002 ld $02 ;$028a = 650 = floor(51 ** 2 / 4); $028a >> 8 = $02
|
|
0014 0002 ld $02 ;$02a4 = 676 = floor(52 ** 2 / 4); $02a4 >> 8 = $02
|
|
0015 0002 ld $02 ;$02be = 702 = floor(53 ** 2 / 4); $02be >> 8 = $02
|
|
0016 0002 ld $02 ;$02d9 = 729 = floor(54 ** 2 / 4); $02d9 >> 8 = $02
|
|
0017 0002 ld $02 ;$02f4 = 756 = floor(55 ** 2 / 4); $02f4 >> 8 = $02
|
|
0018 0003 ld $03 ;$0310 = 784 = floor(56 ** 2 / 4); $0310 >> 8 = $03
|
|
0019 0003 ld $03 ;$032c = 812 = floor(57 ** 2 / 4); $032c >> 8 = $03
|
|
001a 0003 ld $03 ;$0349 = 841 = floor(58 ** 2 / 4); $0349 >> 8 = $03
|
|
001b 0003 ld $03 ;$0366 = 870 = floor(59 ** 2 / 4); $0366 >> 8 = $03
|
|
001c 0003 ld $03 ;$0384 = 900 = floor(60 ** 2 / 4); $0384 >> 8 = $03
|
|
001d 0003 ld $03 ;$03a2 = 930 = floor(61 ** 2 / 4); $03a2 >> 8 = $03
|
|
001e 0003 ld $03 ;$03c1 = 961 = floor(62 ** 2 / 4); $03c1 >> 8 = $03
|
|
001f 0003 ld $03 ;$03e0 = 992 = floor(63 ** 2 / 4); $03e0 >> 8 = $03
|
|
0020 0004 ld $04 ;$0400 = 1024 = floor(64 ** 2 / 4); $0400 >> 8 = $04
|
|
0021 0004 ld $04 ;$0420 = 1056 = floor(65 ** 2 / 4); $0420 >> 8 = $04
|
|
0022 0004 ld $04 ;$0441 = 1089 = floor(66 ** 2 / 4); $0441 >> 8 = $04
|
|
0023 0004 ld $04 ;$0462 = 1122 = floor(67 ** 2 / 4); $0462 >> 8 = $04
|
|
0024 0004 ld $04 ;$0484 = 1156 = floor(68 ** 2 / 4); $0484 >> 8 = $04
|
|
0025 0004 ld $04 ;$04a6 = 1190 = floor(69 ** 2 / 4); $04a6 >> 8 = $04
|
|
0026 0004 ld $04 ;$04c9 = 1225 = floor(70 ** 2 / 4); $04c9 >> 8 = $04
|
|
0027 0004 ld $04 ;$04ec = 1260 = floor(71 ** 2 / 4); $04ec >> 8 = $04
|
|
0028 0005 ld $05 ;$0510 = 1296 = floor(72 ** 2 / 4); $0510 >> 8 = $05
|
|
0029 0005 ld $05 ;$0534 = 1332 = floor(73 ** 2 / 4); $0534 >> 8 = $05
|
|
002a 0005 ld $05 ;$0559 = 1369 = floor(74 ** 2 / 4); $0559 >> 8 = $05
|
|
002b 0005 ld $05 ;$057e = 1406 = floor(75 ** 2 / 4); $057e >> 8 = $05
|
|
002c 0005 ld $05 ;$05a4 = 1444 = floor(76 ** 2 / 4); $05a4 >> 8 = $05
|
|
002d 0005 ld $05 ;$05ca = 1482 = floor(77 ** 2 / 4); $05ca >> 8 = $05
|
|
002e 0005 ld $05 ;$05f1 = 1521 = floor(78 ** 2 / 4); $05f1 >> 8 = $05
|
|
002f 0006 ld $06 ;$0618 = 1560 = floor(79 ** 2 / 4); $0618 >> 8 = $06
|
|
0030 0006 ld $06 ;$0640 = 1600 = floor(80 ** 2 / 4); $0640 >> 8 = $06
|
|
0031 0006 ld $06 ;$0668 = 1640 = floor(81 ** 2 / 4); $0668 >> 8 = $06
|
|
0032 0006 ld $06 ;$0691 = 1681 = floor(82 ** 2 / 4); $0691 >> 8 = $06
|
|
0033 0006 ld $06 ;$06ba = 1722 = floor(83 ** 2 / 4); $06ba >> 8 = $06
|
|
0034 0006 ld $06 ;$06e4 = 1764 = floor(84 ** 2 / 4); $06e4 >> 8 = $06
|
|
0035 0007 ld $07 ;$070e = 1806 = floor(85 ** 2 / 4); $070e >> 8 = $07
|
|
0036 0007 ld $07 ;$0739 = 1849 = floor(86 ** 2 / 4); $0739 >> 8 = $07
|
|
0037 0007 ld $07 ;$0764 = 1892 = floor(87 ** 2 / 4); $0764 >> 8 = $07
|
|
0038 0007 ld $07 ;$0790 = 1936 = floor(88 ** 2 / 4); $0790 >> 8 = $07
|
|
0039 0007 ld $07 ;$07bc = 1980 = floor(89 ** 2 / 4); $07bc >> 8 = $07
|
|
003a 0007 ld $07 ;$07e9 = 2025 = floor(90 ** 2 / 4); $07e9 >> 8 = $07
|
|
003b 0008 ld $08 ;$0816 = 2070 = floor(91 ** 2 / 4); $0816 >> 8 = $08
|
|
003c 0008 ld $08 ;$0844 = 2116 = floor(92 ** 2 / 4); $0844 >> 8 = $08
|
|
003d 0008 ld $08 ;$0872 = 2162 = floor(93 ** 2 / 4); $0872 >> 8 = $08
|
|
003e 0008 ld $08 ;$08a1 = 2209 = floor(94 ** 2 / 4); $08a1 >> 8 = $08
|
|
003f 0008 ld $08 ;$08d0 = 2256 = floor(95 ** 2 / 4); $08d0 >> 8 = $08
|
|
0040 0009 ld $09 ;$0900 = 2304 = floor(96 ** 2 / 4); $0900 >> 8 = $09
|
|
0041 0009 ld $09 ;$0930 = 2352 = floor(97 ** 2 / 4); $0930 >> 8 = $09
|
|
0042 0009 ld $09 ;$0961 = 2401 = floor(98 ** 2 / 4); $0961 >> 8 = $09
|
|
0043 0009 ld $09 ;$0992 = 2450 = floor(99 ** 2 / 4); $0992 >> 8 = $09
|
|
0044 0009 ld $09 ;$09c4 = 2500 = floor(100 ** 2 / 4); $09c4 >> 8 = $09
|
|
0045 0009 ld $09 ;$09f6 = 2550 = floor(101 ** 2 / 4); $09f6 >> 8 = $09
|
|
0046 000a ld $0a ;$0a29 = 2601 = floor(102 ** 2 / 4); $0a29 >> 8 = $0a
|
|
0047 000a ld $0a ;$0a5c = 2652 = floor(103 ** 2 / 4); $0a5c >> 8 = $0a
|
|
0048 000a ld $0a ;$0a90 = 2704 = floor(104 ** 2 / 4); $0a90 >> 8 = $0a
|
|
0049 000a ld $0a ;$0ac4 = 2756 = floor(105 ** 2 / 4); $0ac4 >> 8 = $0a
|
|
004a 000a ld $0a ;$0af9 = 2809 = floor(106 ** 2 / 4); $0af9 >> 8 = $0a
|
|
004b 000b ld $0b ;$0b2e = 2862 = floor(107 ** 2 / 4); $0b2e >> 8 = $0b
|
|
004c 000b ld $0b ;$0b64 = 2916 = floor(108 ** 2 / 4); $0b64 >> 8 = $0b
|
|
004d 000b ld $0b ;$0b9a = 2970 = floor(109 ** 2 / 4); $0b9a >> 8 = $0b
|
|
004e 000b ld $0b ;$0bd1 = 3025 = floor(110 ** 2 / 4); $0bd1 >> 8 = $0b
|
|
004f 000c ld $0c ;$0c08 = 3080 = floor(111 ** 2 / 4); $0c08 >> 8 = $0c
|
|
0050 000c ld $0c ;$0c40 = 3136 = floor(112 ** 2 / 4); $0c40 >> 8 = $0c
|
|
0051 000c ld $0c ;$0c78 = 3192 = floor(113 ** 2 / 4); $0c78 >> 8 = $0c
|
|
0052 000c ld $0c ;$0cb1 = 3249 = floor(114 ** 2 / 4); $0cb1 >> 8 = $0c
|
|
0053 000c ld $0c ;$0cea = 3306 = floor(115 ** 2 / 4); $0cea >> 8 = $0c
|
|
0054 000d ld $0d ;$0d24 = 3364 = floor(116 ** 2 / 4); $0d24 >> 8 = $0d
|
|
0055 000d ld $0d ;$0d5e = 3422 = floor(117 ** 2 / 4); $0d5e >> 8 = $0d
|
|
0056 000d ld $0d ;$0d99 = 3481 = floor(118 ** 2 / 4); $0d99 >> 8 = $0d
|
|
0057 000d ld $0d ;$0dd4 = 3540 = floor(119 ** 2 / 4); $0dd4 >> 8 = $0d
|
|
0058 000e ld $0e ;$0e10 = 3600 = floor(120 ** 2 / 4); $0e10 >> 8 = $0e
|
|
0059 000e ld $0e ;$0e4c = 3660 = floor(121 ** 2 / 4); $0e4c >> 8 = $0e
|
|
005a 000e ld $0e ;$0e89 = 3721 = floor(122 ** 2 / 4); $0e89 >> 8 = $0e
|
|
005b 000e ld $0e ;$0ec6 = 3782 = floor(123 ** 2 / 4); $0ec6 >> 8 = $0e
|
|
005c 000f ld $0f ;$0f04 = 3844 = floor(124 ** 2 / 4); $0f04 >> 8 = $0f
|
|
005d 000f ld $0f ;$0f42 = 3906 = floor(125 ** 2 / 4); $0f42 >> 8 = $0f
|
|
005e 000f ld $0f ;$0f81 = 3969 = floor(126 ** 2 / 4); $0f81 >> 8 = $0f
|
|
005f 000f ld $0f ;$0fc0 = 4032 = floor(127 ** 2 / 4); $0fc0 >> 8 = $0f
|
|
0060 0010 ld $10 ;$1000 = 4096 = floor(128 ** 2 / 4); $1000 >> 8 = $10
|
|
0061 0010 ld $10 ;$1040 = 4160 = floor(129 ** 2 / 4); $1040 >> 8 = $10
|
|
0062 0010 ld $10 ;$1081 = 4225 = floor(130 ** 2 / 4); $1081 >> 8 = $10
|
|
0063 0010 ld $10 ;$10c2 = 4290 = floor(131 ** 2 / 4); $10c2 >> 8 = $10
|
|
0064 0011 ld $11 ;$1104 = 4356 = floor(132 ** 2 / 4); $1104 >> 8 = $11
|
|
0065 0011 ld $11 ;$1146 = 4422 = floor(133 ** 2 / 4); $1146 >> 8 = $11
|
|
0066 0011 ld $11 ;$1189 = 4489 = floor(134 ** 2 / 4); $1189 >> 8 = $11
|
|
0067 0011 ld $11 ;$11cc = 4556 = floor(135 ** 2 / 4); $11cc >> 8 = $11
|
|
0068 0012 ld $12 ;$1210 = 4624 = floor(136 ** 2 / 4); $1210 >> 8 = $12
|
|
0069 0012 ld $12 ;$1254 = 4692 = floor(137 ** 2 / 4); $1254 >> 8 = $12
|
|
006a 0012 ld $12 ;$1299 = 4761 = floor(138 ** 2 / 4); $1299 >> 8 = $12
|
|
006b 0012 ld $12 ;$12de = 4830 = floor(139 ** 2 / 4); $12de >> 8 = $12
|
|
006c 0013 ld $13 ;$1324 = 4900 = floor(140 ** 2 / 4); $1324 >> 8 = $13
|
|
006d 0013 ld $13 ;$136a = 4970 = floor(141 ** 2 / 4); $136a >> 8 = $13
|
|
006e 0013 ld $13 ;$13b1 = 5041 = floor(142 ** 2 / 4); $13b1 >> 8 = $13
|
|
006f 0013 ld $13 ;$13f8 = 5112 = floor(143 ** 2 / 4); $13f8 >> 8 = $13
|
|
0070 0014 ld $14 ;$1440 = 5184 = floor(144 ** 2 / 4); $1440 >> 8 = $14
|
|
0071 0014 ld $14 ;$1488 = 5256 = floor(145 ** 2 / 4); $1488 >> 8 = $14
|
|
0072 0014 ld $14 ;$14d1 = 5329 = floor(146 ** 2 / 4); $14d1 >> 8 = $14
|
|
0073 0015 ld $15 ;$151a = 5402 = floor(147 ** 2 / 4); $151a >> 8 = $15
|
|
0074 0015 ld $15 ;$1564 = 5476 = floor(148 ** 2 / 4); $1564 >> 8 = $15
|
|
0075 0015 ld $15 ;$15ae = 5550 = floor(149 ** 2 / 4); $15ae >> 8 = $15
|
|
0076 0015 ld $15 ;$15f9 = 5625 = floor(150 ** 2 / 4); $15f9 >> 8 = $15
|
|
0077 0016 ld $16 ;$1644 = 5700 = floor(151 ** 2 / 4); $1644 >> 8 = $16
|
|
0078 0016 ld $16 ;$1690 = 5776 = floor(152 ** 2 / 4); $1690 >> 8 = $16
|
|
0079 0016 ld $16 ;$16dc = 5852 = floor(153 ** 2 / 4); $16dc >> 8 = $16
|
|
007a 0017 ld $17 ;$1729 = 5929 = floor(154 ** 2 / 4); $1729 >> 8 = $17
|
|
007b 0017 ld $17 ;$1776 = 6006 = floor(155 ** 2 / 4); $1776 >> 8 = $17
|
|
007c 0017 ld $17 ;$17c4 = 6084 = floor(156 ** 2 / 4); $17c4 >> 8 = $17
|
|
007d 0018 ld $18 ;$1812 = 6162 = floor(157 ** 2 / 4); $1812 >> 8 = $18
|
|
007e 0018 ld $18 ;$1861 = 6241 = floor(158 ** 2 / 4); $1861 >> 8 = $18
|
|
007f 0018 ld $18 ;$18b0 = 6320 = floor(159 ** 2 / 4); $18b0 >> 8 = $18
|
|
0080 0019 ld $19 ;$1900 = 6400 = floor(160 ** 2 / 4); $1900 >> 8 = $19
|
|
0081 0019 ld $19 ;$1950 = 6480 = floor(161 ** 2 / 4); $1950 >> 8 = $19
|
|
0082 0019 ld $19 ;$19a1 = 6561 = floor(162 ** 2 / 4); $19a1 >> 8 = $19
|
|
0083 0019 ld $19 ;$19f2 = 6642 = floor(163 ** 2 / 4); $19f2 >> 8 = $19
|
|
0084 001a ld $1a ;$1a44 = 6724 = floor(164 ** 2 / 4); $1a44 >> 8 = $1a
|
|
0085 001a ld $1a ;$1a96 = 6806 = floor(165 ** 2 / 4); $1a96 >> 8 = $1a
|
|
0086 001a ld $1a ;$1ae9 = 6889 = floor(166 ** 2 / 4); $1ae9 >> 8 = $1a
|
|
0087 001b ld $1b ;$1b3c = 6972 = floor(167 ** 2 / 4); $1b3c >> 8 = $1b
|
|
0088 001b ld $1b ;$1b90 = 7056 = floor(168 ** 2 / 4); $1b90 >> 8 = $1b
|
|
0089 001b ld $1b ;$1be4 = 7140 = floor(169 ** 2 / 4); $1be4 >> 8 = $1b
|
|
008a 001c ld $1c ;$1c39 = 7225 = floor(170 ** 2 / 4); $1c39 >> 8 = $1c
|
|
008b 001c ld $1c ;$1c8e = 7310 = floor(171 ** 2 / 4); $1c8e >> 8 = $1c
|
|
008c 001c ld $1c ;$1ce4 = 7396 = floor(172 ** 2 / 4); $1ce4 >> 8 = $1c
|
|
008d 001d ld $1d ;$1d3a = 7482 = floor(173 ** 2 / 4); $1d3a >> 8 = $1d
|
|
008e 001d ld $1d ;$1d91 = 7569 = floor(174 ** 2 / 4); $1d91 >> 8 = $1d
|
|
008f 001d ld $1d ;$1de8 = 7656 = floor(175 ** 2 / 4); $1de8 >> 8 = $1d
|
|
0090 001e ld $1e ;$1e40 = 7744 = floor(176 ** 2 / 4); $1e40 >> 8 = $1e
|
|
0091 001e ld $1e ;$1e98 = 7832 = floor(177 ** 2 / 4); $1e98 >> 8 = $1e
|
|
0092 001e ld $1e ;$1ef1 = 7921 = floor(178 ** 2 / 4); $1ef1 >> 8 = $1e
|
|
0093 001f ld $1f ;$1f4a = 8010 = floor(179 ** 2 / 4); $1f4a >> 8 = $1f
|
|
0094 001f ld $1f ;$1fa4 = 8100 = floor(180 ** 2 / 4); $1fa4 >> 8 = $1f
|
|
0095 001f ld $1f ;$1ffe = 8190 = floor(181 ** 2 / 4); $1ffe >> 8 = $1f
|
|
0096 0020 ld $20 ;$2059 = 8281 = floor(182 ** 2 / 4); $2059 >> 8 = $20
|
|
0097 0020 ld $20 ;$20b4 = 8372 = floor(183 ** 2 / 4); $20b4 >> 8 = $20
|
|
0098 0021 ld $21 ;$2110 = 8464 = floor(184 ** 2 / 4); $2110 >> 8 = $21
|
|
0099 0021 ld $21 ;$216c = 8556 = floor(185 ** 2 / 4); $216c >> 8 = $21
|
|
009a 0021 ld $21 ;$21c9 = 8649 = floor(186 ** 2 / 4); $21c9 >> 8 = $21
|
|
009b 0022 ld $22 ;$2226 = 8742 = floor(187 ** 2 / 4); $2226 >> 8 = $22
|
|
009c 0022 ld $22 ;$2284 = 8836 = floor(188 ** 2 / 4); $2284 >> 8 = $22
|
|
009d 0022 ld $22 ;$22e2 = 8930 = floor(189 ** 2 / 4); $22e2 >> 8 = $22
|
|
009e 0023 ld $23 ;$2341 = 9025 = floor(190 ** 2 / 4); $2341 >> 8 = $23
|
|
009f 0023 ld $23 ;$23a0 = 9120 = floor(191 ** 2 / 4); $23a0 >> 8 = $23
|
|
00a0 0024 ld $24 ;$2400 = 9216 = floor(192 ** 2 / 4); $2400 >> 8 = $24
|
|
00a1 0024 ld $24 ;$2460 = 9312 = floor(193 ** 2 / 4); $2460 >> 8 = $24
|
|
00a2 0024 ld $24 ;$24c1 = 9409 = floor(194 ** 2 / 4); $24c1 >> 8 = $24
|
|
00a3 0025 ld $25 ;$2522 = 9506 = floor(195 ** 2 / 4); $2522 >> 8 = $25
|
|
00a4 0025 ld $25 ;$2584 = 9604 = floor(196 ** 2 / 4); $2584 >> 8 = $25
|
|
00a5 0025 ld $25 ;$25e6 = 9702 = floor(197 ** 2 / 4); $25e6 >> 8 = $25
|
|
00a6 0026 ld $26 ;$2649 = 9801 = floor(198 ** 2 / 4); $2649 >> 8 = $26
|
|
00a7 0026 ld $26 ;$26ac = 9900 = floor(199 ** 2 / 4); $26ac >> 8 = $26
|
|
00a8 0027 ld $27 ;$2710 = 10000 = floor(200 ** 2 / 4); $2710 >> 8 = $27
|
|
00a9 0027 ld $27 ;$2774 = 10100 = floor(201 ** 2 / 4); $2774 >> 8 = $27
|
|
00aa 0027 ld $27 ;$27d9 = 10201 = floor(202 ** 2 / 4); $27d9 >> 8 = $27
|
|
00ab 0028 ld $28 ;$283e = 10302 = floor(203 ** 2 / 4); $283e >> 8 = $28
|
|
00ac 0028 ld $28 ;$28a4 = 10404 = floor(204 ** 2 / 4); $28a4 >> 8 = $28
|
|
00ad 0029 ld $29 ;$290a = 10506 = floor(205 ** 2 / 4); $290a >> 8 = $29
|
|
00ae 0029 ld $29 ;$2971 = 10609 = floor(206 ** 2 / 4); $2971 >> 8 = $29
|
|
00af 0029 ld $29 ;$29d8 = 10712 = floor(207 ** 2 / 4); $29d8 >> 8 = $29
|
|
00b0 002a ld $2a ;$2a40 = 10816 = floor(208 ** 2 / 4); $2a40 >> 8 = $2a
|
|
00b1 002a ld $2a ;$2aa8 = 10920 = floor(209 ** 2 / 4); $2aa8 >> 8 = $2a
|
|
00b2 002b ld $2b ;$2b11 = 11025 = floor(210 ** 2 / 4); $2b11 >> 8 = $2b
|
|
00b3 002b ld $2b ;$2b7a = 11130 = floor(211 ** 2 / 4); $2b7a >> 8 = $2b
|
|
00b4 002b ld $2b ;$2be4 = 11236 = floor(212 ** 2 / 4); $2be4 >> 8 = $2b
|
|
00b5 002c ld $2c ;$2c4e = 11342 = floor(213 ** 2 / 4); $2c4e >> 8 = $2c
|
|
00b6 002c ld $2c ;$2cb9 = 11449 = floor(214 ** 2 / 4); $2cb9 >> 8 = $2c
|
|
00b7 002d ld $2d ;$2d24 = 11556 = floor(215 ** 2 / 4); $2d24 >> 8 = $2d
|
|
00b8 002d ld $2d ;$2d90 = 11664 = floor(216 ** 2 / 4); $2d90 >> 8 = $2d
|
|
00b9 002d ld $2d ;$2dfc = 11772 = floor(217 ** 2 / 4); $2dfc >> 8 = $2d
|
|
00ba 002e ld $2e ;$2e69 = 11881 = floor(218 ** 2 / 4); $2e69 >> 8 = $2e
|
|
00bb 002e ld $2e ;$2ed6 = 11990 = floor(219 ** 2 / 4); $2ed6 >> 8 = $2e
|
|
00bc 002f ld $2f ;$2f44 = 12100 = floor(220 ** 2 / 4); $2f44 >> 8 = $2f
|
|
00bd 002f ld $2f ;$2fb2 = 12210 = floor(221 ** 2 / 4); $2fb2 >> 8 = $2f
|
|
00be 0030 ld $30 ;$3021 = 12321 = floor(222 ** 2 / 4); $3021 >> 8 = $30
|
|
00bf 0030 ld $30 ;$3090 = 12432 = floor(223 ** 2 / 4); $3090 >> 8 = $30
|
|
00c0 0031 ld $31 ;$3100 = 12544 = floor(224 ** 2 / 4); $3100 >> 8 = $31
|
|
00c1 0031 ld $31 ;$3170 = 12656 = floor(225 ** 2 / 4); $3170 >> 8 = $31
|
|
00c2 0031 ld $31 ;$31e1 = 12769 = floor(226 ** 2 / 4); $31e1 >> 8 = $31
|
|
00c3 0032 ld $32 ;$3252 = 12882 = floor(227 ** 2 / 4); $3252 >> 8 = $32
|
|
00c4 0032 ld $32 ;$32c4 = 12996 = floor(228 ** 2 / 4); $32c4 >> 8 = $32
|
|
00c5 0033 ld $33 ;$3336 = 13110 = floor(229 ** 2 / 4); $3336 >> 8 = $33
|
|
00c6 0033 ld $33 ;$33a9 = 13225 = floor(230 ** 2 / 4); $33a9 >> 8 = $33
|
|
00c7 0034 ld $34 ;$341c = 13340 = floor(231 ** 2 / 4); $341c >> 8 = $34
|
|
00c8 0034 ld $34 ;$3490 = 13456 = floor(232 ** 2 / 4); $3490 >> 8 = $34
|
|
00c9 0035 ld $35 ;$3504 = 13572 = floor(233 ** 2 / 4); $3504 >> 8 = $35
|
|
00ca 0035 ld $35 ;$3579 = 13689 = floor(234 ** 2 / 4); $3579 >> 8 = $35
|
|
00cb 0035 ld $35 ;$35ee = 13806 = floor(235 ** 2 / 4); $35ee >> 8 = $35
|
|
00cc 0036 ld $36 ;$3664 = 13924 = floor(236 ** 2 / 4); $3664 >> 8 = $36
|
|
00cd 0036 ld $36 ;$36da = 14042 = floor(237 ** 2 / 4); $36da >> 8 = $36
|
|
00ce 0037 ld $37 ;$3751 = 14161 = floor(238 ** 2 / 4); $3751 >> 8 = $37
|
|
00cf 0037 ld $37 ;$37c8 = 14280 = floor(239 ** 2 / 4); $37c8 >> 8 = $37
|
|
00d0 0038 ld $38 ;$3840 = 14400 = floor(240 ** 2 / 4); $3840 >> 8 = $38
|
|
00d1 0038 ld $38 ;$38b8 = 14520 = floor(241 ** 2 / 4); $38b8 >> 8 = $38
|
|
00d2 0039 ld $39 ;$3931 = 14641 = floor(242 ** 2 / 4); $3931 >> 8 = $39
|
|
00d3 0039 ld $39 ;$39aa = 14762 = floor(243 ** 2 / 4); $39aa >> 8 = $39
|
|
00d4 003a ld $3a ;$3a24 = 14884 = floor(244 ** 2 / 4); $3a24 >> 8 = $3a
|
|
00d5 003a ld $3a ;$3a9e = 15006 = floor(245 ** 2 / 4); $3a9e >> 8 = $3a
|
|
00d6 003b ld $3b ;$3b19 = 15129 = floor(246 ** 2 / 4); $3b19 >> 8 = $3b
|
|
00d7 003b ld $3b ;$3b94 = 15252 = floor(247 ** 2 / 4); $3b94 >> 8 = $3b
|
|
00d8 003c ld $3c ;$3c10 = 15376 = floor(248 ** 2 / 4); $3c10 >> 8 = $3c
|
|
00d9 003c ld $3c ;$3c8c = 15500 = floor(249 ** 2 / 4); $3c8c >> 8 = $3c
|
|
00da 003d ld $3d ;$3d09 = 15625 = floor(250 ** 2 / 4); $3d09 >> 8 = $3d
|
|
00db 003d ld $3d ;$3d86 = 15750 = floor(251 ** 2 / 4); $3d86 >> 8 = $3d
|
|
00dc 003e ld $3e ;$3e04 = 15876 = floor(252 ** 2 / 4); $3e04 >> 8 = $3e
|
|
00dd 003e ld $3e ;$3e82 = 16002 = floor(253 ** 2 / 4); $3e82 >> 8 = $3e
|
|
00de 003f ld $3f ;$3f01 = 16129 = floor(254 ** 2 / 4); $3f01 >> 8 = $3f
|
|
00df 003f ld $3f ;$3f80 = 16256 = floor(255 ** 2 / 4); $3f80 >> 8 = $3f
|
|
62 C(f"${val:04x} = {val} = floor({i} ** 2 / 4); ${val:04x} >> 8 = ${val >> 8:02x}")
|
|
63
|
|
64 # We jump back here after looking up the low-byte of the result.
|
|
65 label("low-byte return point")
|
|
low-byte return point:
|
|
00e0 1403 ld $03,y 66 ld(hi("multiply 7x7"), Y)
|
|
00e1 e106 jmp y,[$06] 67 jmp(Y, [continuation])
|
|
00e2 1400 ld $00,y 68 ld(hi(pc()), Y) # Make it easy to get back here!
|
|
69 cost_of_low_byte_return = 3
|
|
70 label("table entry.possibly-negative")
|
|
71 # AC is negative, if b > a. Find absolute value
|
|
table entry.possibly-negative:
|
|
00e3 e8e6 blt $00e6 72 blt(pc() + 3) # 1
|
|
00e4 fce7 bra $00e7 73 bra(pc() + 3) # 2
|
|
00e5 a001 suba $01 74 suba(1) # 3; if >= 0
|
|
00e6 60ff xora $ff 75 xora(0xFF) # 3; if < 0
|
|
00e7 8001 adda $01 76 adda(1) # 4
|
|
77 cost_of_absolute = 4
|
|
78 label("table entry")
|
|
79 # Calculate an index into the high-byte table.
|
|
80 # This is basically a matter of subtracting 32, and jumping in if the result >= 0.
|
|
81 # But values greater than 160 have the sign-bit set after subtraction,
|
|
82 # despite being >32.
|
|
83 # We test for the sign bit and jump after subtraction even if 'negative' in these cases.
|
|
table entry: 00e8 c205 st [$05] 84 st([tmp]) # 1
|
|
00e9 e8ee blt $00ee 85 blt(pc() + 5) # 2
|
|
00ea a020 suba $20 86 suba(32) # 3
|
|
00eb f600 bge ac 87 bge(AC) # 4
|
|
00ec fd07 bra [$07] 88 bra([high_byte_action]) # 5
|
|
00ed 0000 ld $00 89 ld(0) # 6
|
|
00ee fe00 bra ac 90 bra(AC) # 4
|
|
00ef fd07 bra [$07] 91 bra([high_byte_action]) # 5
|
|
92 cost_of_high_byte_table_entry = 6
|
|
93 # Some space here for other code?
|
|
00f0 0200 nop 94 fillers(until=251)
|
|
00f1 0200 nop
|
|
00f2 0200 nop
|
|
* 11 times
|
|
95 # We jump back here after looking up the high-byte of the result.
|
|
96 # Counting is in reverse
|
|
97 label("high-byte action.invert-and-add")
|
|
high-byte action.invert-and-add:
|
|
00fb 60ff xora $ff 98 xora(0xFF) # 4
|
|
99 label("high-byte action.add")
|
|
high-byte action.add:
|
|
00fc 8104 adda [$04] 100 adda([result + 1]) # 3
|
|
101 label("high-byte action.store")
|
|
high-byte action.store:
|
|
00fd c204 st [$04] 102 st([result + 1]) # 2
|
|
00fe 0105 ld [$05] 103 ld([tmp]) # 1
|
|
104 assert pc() & 0xFF == 0xFF, pc()
|
|
105 cost_of_high_byte_invert_and_add = 4
|
|
106 cost_of_high_byte_add = 3
|
|
107 cost_of_high_byte_store = 2
|
|
108 label("low-byte table entry")
|
|
109 # With the index in the accumulator, and the current page in the Y register,
|
|
110 # We jump to the right entry in the low-byte table, replace AC with the result,
|
|
111 # and jump back out to 'low-byte return point' defined above,
|
|
112 # using the double-jump trick.
|
|
113 # This exploits the fact the following is the last instruction on the page,
|
|
114 # and the high byte of the PC has already incremented,
|
|
115 # so the bne will take us to an address in the next page.
|
|
116 # We use the Y register to return to the current page.
|
|
117 # The table has two zeros at the start, meaning that we can replace the first
|
|
118 # with the jump back.
|
|
low-byte table entry:
|
|
00ff ee00 bne ac 119 bne(AC) # 1
|
|
120 align(0x100, size=0x100)
|
|
0100 e0e0 jmp y,$e0 121 jmp(Y, "low-byte return point") # 2
|
|
122 ld(0) # 3
|
|
0101 0000 ld $00 ;0 = floor(0 ** 2 / 4) and floor(1 ** 2 / 4)
|
|
123 cost_of_low_byte_table_entry = 3
|
|
124 C("0 = floor(0 ** 2 / 4) and floor(1 ** 2 / 4)")
|
|
125 for i in range(2, 256):
|
|
126 val = math.floor(i**2 / 4)
|
|
127 ld(val)
|
|
0102 0001 ld $01 ;$0001 = 1 = floor(2 ** 2 / 4)
|
|
0103 0002 ld $02 ;$0002 = 2 = floor(3 ** 2 / 4)
|
|
0104 0004 ld $04 ;$0004 = 4 = floor(4 ** 2 / 4)
|
|
0105 0006 ld $06 ;$0006 = 6 = floor(5 ** 2 / 4)
|
|
0106 0009 ld $09 ;$0009 = 9 = floor(6 ** 2 / 4)
|
|
0107 000c ld $0c ;$000c = 12 = floor(7 ** 2 / 4)
|
|
0108 0010 ld $10 ;$0010 = 16 = floor(8 ** 2 / 4)
|
|
0109 0014 ld $14 ;$0014 = 20 = floor(9 ** 2 / 4)
|
|
010a 0019 ld $19 ;$0019 = 25 = floor(10 ** 2 / 4)
|
|
010b 001e ld $1e ;$001e = 30 = floor(11 ** 2 / 4)
|
|
010c 0024 ld $24 ;$0024 = 36 = floor(12 ** 2 / 4)
|
|
010d 002a ld $2a ;$002a = 42 = floor(13 ** 2 / 4)
|
|
010e 0031 ld $31 ;$0031 = 49 = floor(14 ** 2 / 4)
|
|
010f 0038 ld $38 ;$0038 = 56 = floor(15 ** 2 / 4)
|
|
0110 0040 ld $40 ;$0040 = 64 = floor(16 ** 2 / 4)
|
|
0111 0048 ld $48 ;$0048 = 72 = floor(17 ** 2 / 4)
|
|
0112 0051 ld $51 ;$0051 = 81 = floor(18 ** 2 / 4)
|
|
0113 005a ld $5a ;$005a = 90 = floor(19 ** 2 / 4)
|
|
0114 0064 ld $64 ;$0064 = 100 = floor(20 ** 2 / 4)
|
|
0115 006e ld $6e ;$006e = 110 = floor(21 ** 2 / 4)
|
|
0116 0079 ld $79 ;$0079 = 121 = floor(22 ** 2 / 4)
|
|
0117 0084 ld $84 ;$0084 = 132 = floor(23 ** 2 / 4)
|
|
0118 0090 ld $90 ;$0090 = 144 = floor(24 ** 2 / 4)
|
|
0119 009c ld $9c ;$009c = 156 = floor(25 ** 2 / 4)
|
|
011a 00a9 ld $a9 ;$00a9 = 169 = floor(26 ** 2 / 4)
|
|
011b 00b6 ld $b6 ;$00b6 = 182 = floor(27 ** 2 / 4)
|
|
011c 00c4 ld $c4 ;$00c4 = 196 = floor(28 ** 2 / 4)
|
|
011d 00d2 ld $d2 ;$00d2 = 210 = floor(29 ** 2 / 4)
|
|
011e 00e1 ld $e1 ;$00e1 = 225 = floor(30 ** 2 / 4)
|
|
011f 00f0 ld $f0 ;$00f0 = 240 = floor(31 ** 2 / 4)
|
|
0120 0000 ld $00 ;$0100 = 256 = floor(32 ** 2 / 4)
|
|
0121 0010 ld $10 ;$0110 = 272 = floor(33 ** 2 / 4)
|
|
0122 0021 ld $21 ;$0121 = 289 = floor(34 ** 2 / 4)
|
|
0123 0032 ld $32 ;$0132 = 306 = floor(35 ** 2 / 4)
|
|
0124 0044 ld $44 ;$0144 = 324 = floor(36 ** 2 / 4)
|
|
0125 0056 ld $56 ;$0156 = 342 = floor(37 ** 2 / 4)
|
|
0126 0069 ld $69 ;$0169 = 361 = floor(38 ** 2 / 4)
|
|
0127 007c ld $7c ;$017c = 380 = floor(39 ** 2 / 4)
|
|
0128 0090 ld $90 ;$0190 = 400 = floor(40 ** 2 / 4)
|
|
0129 00a4 ld $a4 ;$01a4 = 420 = floor(41 ** 2 / 4)
|
|
012a 00b9 ld $b9 ;$01b9 = 441 = floor(42 ** 2 / 4)
|
|
012b 00ce ld $ce ;$01ce = 462 = floor(43 ** 2 / 4)
|
|
012c 00e4 ld $e4 ;$01e4 = 484 = floor(44 ** 2 / 4)
|
|
012d 00fa ld $fa ;$01fa = 506 = floor(45 ** 2 / 4)
|
|
012e 0011 ld $11 ;$0211 = 529 = floor(46 ** 2 / 4)
|
|
012f 0028 ld $28 ;$0228 = 552 = floor(47 ** 2 / 4)
|
|
0130 0040 ld $40 ;$0240 = 576 = floor(48 ** 2 / 4)
|
|
0131 0058 ld $58 ;$0258 = 600 = floor(49 ** 2 / 4)
|
|
0132 0071 ld $71 ;$0271 = 625 = floor(50 ** 2 / 4)
|
|
0133 008a ld $8a ;$028a = 650 = floor(51 ** 2 / 4)
|
|
0134 00a4 ld $a4 ;$02a4 = 676 = floor(52 ** 2 / 4)
|
|
0135 00be ld $be ;$02be = 702 = floor(53 ** 2 / 4)
|
|
0136 00d9 ld $d9 ;$02d9 = 729 = floor(54 ** 2 / 4)
|
|
0137 00f4 ld $f4 ;$02f4 = 756 = floor(55 ** 2 / 4)
|
|
0138 0010 ld $10 ;$0310 = 784 = floor(56 ** 2 / 4)
|
|
0139 002c ld $2c ;$032c = 812 = floor(57 ** 2 / 4)
|
|
013a 0049 ld $49 ;$0349 = 841 = floor(58 ** 2 / 4)
|
|
013b 0066 ld $66 ;$0366 = 870 = floor(59 ** 2 / 4)
|
|
013c 0084 ld $84 ;$0384 = 900 = floor(60 ** 2 / 4)
|
|
013d 00a2 ld $a2 ;$03a2 = 930 = floor(61 ** 2 / 4)
|
|
013e 00c1 ld $c1 ;$03c1 = 961 = floor(62 ** 2 / 4)
|
|
013f 00e0 ld $e0 ;$03e0 = 992 = floor(63 ** 2 / 4)
|
|
0140 0000 ld $00 ;$0400 = 1024 = floor(64 ** 2 / 4)
|
|
0141 0020 ld $20 ;$0420 = 1056 = floor(65 ** 2 / 4)
|
|
0142 0041 ld $41 ;$0441 = 1089 = floor(66 ** 2 / 4)
|
|
0143 0062 ld $62 ;$0462 = 1122 = floor(67 ** 2 / 4)
|
|
0144 0084 ld $84 ;$0484 = 1156 = floor(68 ** 2 / 4)
|
|
0145 00a6 ld $a6 ;$04a6 = 1190 = floor(69 ** 2 / 4)
|
|
0146 00c9 ld $c9 ;$04c9 = 1225 = floor(70 ** 2 / 4)
|
|
0147 00ec ld $ec ;$04ec = 1260 = floor(71 ** 2 / 4)
|
|
0148 0010 ld $10 ;$0510 = 1296 = floor(72 ** 2 / 4)
|
|
0149 0034 ld $34 ;$0534 = 1332 = floor(73 ** 2 / 4)
|
|
014a 0059 ld $59 ;$0559 = 1369 = floor(74 ** 2 / 4)
|
|
014b 007e ld $7e ;$057e = 1406 = floor(75 ** 2 / 4)
|
|
014c 00a4 ld $a4 ;$05a4 = 1444 = floor(76 ** 2 / 4)
|
|
014d 00ca ld $ca ;$05ca = 1482 = floor(77 ** 2 / 4)
|
|
014e 00f1 ld $f1 ;$05f1 = 1521 = floor(78 ** 2 / 4)
|
|
014f 0018 ld $18 ;$0618 = 1560 = floor(79 ** 2 / 4)
|
|
0150 0040 ld $40 ;$0640 = 1600 = floor(80 ** 2 / 4)
|
|
0151 0068 ld $68 ;$0668 = 1640 = floor(81 ** 2 / 4)
|
|
0152 0091 ld $91 ;$0691 = 1681 = floor(82 ** 2 / 4)
|
|
0153 00ba ld $ba ;$06ba = 1722 = floor(83 ** 2 / 4)
|
|
0154 00e4 ld $e4 ;$06e4 = 1764 = floor(84 ** 2 / 4)
|
|
0155 000e ld $0e ;$070e = 1806 = floor(85 ** 2 / 4)
|
|
0156 0039 ld $39 ;$0739 = 1849 = floor(86 ** 2 / 4)
|
|
0157 0064 ld $64 ;$0764 = 1892 = floor(87 ** 2 / 4)
|
|
0158 0090 ld $90 ;$0790 = 1936 = floor(88 ** 2 / 4)
|
|
0159 00bc ld $bc ;$07bc = 1980 = floor(89 ** 2 / 4)
|
|
015a 00e9 ld $e9 ;$07e9 = 2025 = floor(90 ** 2 / 4)
|
|
015b 0016 ld $16 ;$0816 = 2070 = floor(91 ** 2 / 4)
|
|
015c 0044 ld $44 ;$0844 = 2116 = floor(92 ** 2 / 4)
|
|
015d 0072 ld $72 ;$0872 = 2162 = floor(93 ** 2 / 4)
|
|
015e 00a1 ld $a1 ;$08a1 = 2209 = floor(94 ** 2 / 4)
|
|
015f 00d0 ld $d0 ;$08d0 = 2256 = floor(95 ** 2 / 4)
|
|
0160 0000 ld $00 ;$0900 = 2304 = floor(96 ** 2 / 4)
|
|
0161 0030 ld $30 ;$0930 = 2352 = floor(97 ** 2 / 4)
|
|
0162 0061 ld $61 ;$0961 = 2401 = floor(98 ** 2 / 4)
|
|
0163 0092 ld $92 ;$0992 = 2450 = floor(99 ** 2 / 4)
|
|
0164 00c4 ld $c4 ;$09c4 = 2500 = floor(100 ** 2 / 4)
|
|
0165 00f6 ld $f6 ;$09f6 = 2550 = floor(101 ** 2 / 4)
|
|
0166 0029 ld $29 ;$0a29 = 2601 = floor(102 ** 2 / 4)
|
|
0167 005c ld $5c ;$0a5c = 2652 = floor(103 ** 2 / 4)
|
|
0168 0090 ld $90 ;$0a90 = 2704 = floor(104 ** 2 / 4)
|
|
0169 00c4 ld $c4 ;$0ac4 = 2756 = floor(105 ** 2 / 4)
|
|
016a 00f9 ld $f9 ;$0af9 = 2809 = floor(106 ** 2 / 4)
|
|
016b 002e ld $2e ;$0b2e = 2862 = floor(107 ** 2 / 4)
|
|
016c 0064 ld $64 ;$0b64 = 2916 = floor(108 ** 2 / 4)
|
|
016d 009a ld $9a ;$0b9a = 2970 = floor(109 ** 2 / 4)
|
|
016e 00d1 ld $d1 ;$0bd1 = 3025 = floor(110 ** 2 / 4)
|
|
016f 0008 ld $08 ;$0c08 = 3080 = floor(111 ** 2 / 4)
|
|
0170 0040 ld $40 ;$0c40 = 3136 = floor(112 ** 2 / 4)
|
|
0171 0078 ld $78 ;$0c78 = 3192 = floor(113 ** 2 / 4)
|
|
0172 00b1 ld $b1 ;$0cb1 = 3249 = floor(114 ** 2 / 4)
|
|
0173 00ea ld $ea ;$0cea = 3306 = floor(115 ** 2 / 4)
|
|
0174 0024 ld $24 ;$0d24 = 3364 = floor(116 ** 2 / 4)
|
|
0175 005e ld $5e ;$0d5e = 3422 = floor(117 ** 2 / 4)
|
|
0176 0099 ld $99 ;$0d99 = 3481 = floor(118 ** 2 / 4)
|
|
0177 00d4 ld $d4 ;$0dd4 = 3540 = floor(119 ** 2 / 4)
|
|
0178 0010 ld $10 ;$0e10 = 3600 = floor(120 ** 2 / 4)
|
|
0179 004c ld $4c ;$0e4c = 3660 = floor(121 ** 2 / 4)
|
|
017a 0089 ld $89 ;$0e89 = 3721 = floor(122 ** 2 / 4)
|
|
017b 00c6 ld $c6 ;$0ec6 = 3782 = floor(123 ** 2 / 4)
|
|
017c 0004 ld $04 ;$0f04 = 3844 = floor(124 ** 2 / 4)
|
|
017d 0042 ld $42 ;$0f42 = 3906 = floor(125 ** 2 / 4)
|
|
017e 0081 ld $81 ;$0f81 = 3969 = floor(126 ** 2 / 4)
|
|
017f 00c0 ld $c0 ;$0fc0 = 4032 = floor(127 ** 2 / 4)
|
|
0180 0000 ld $00 ;$1000 = 4096 = floor(128 ** 2 / 4)
|
|
0181 0040 ld $40 ;$1040 = 4160 = floor(129 ** 2 / 4)
|
|
0182 0081 ld $81 ;$1081 = 4225 = floor(130 ** 2 / 4)
|
|
0183 00c2 ld $c2 ;$10c2 = 4290 = floor(131 ** 2 / 4)
|
|
0184 0004 ld $04 ;$1104 = 4356 = floor(132 ** 2 / 4)
|
|
0185 0046 ld $46 ;$1146 = 4422 = floor(133 ** 2 / 4)
|
|
0186 0089 ld $89 ;$1189 = 4489 = floor(134 ** 2 / 4)
|
|
0187 00cc ld $cc ;$11cc = 4556 = floor(135 ** 2 / 4)
|
|
0188 0010 ld $10 ;$1210 = 4624 = floor(136 ** 2 / 4)
|
|
0189 0054 ld $54 ;$1254 = 4692 = floor(137 ** 2 / 4)
|
|
018a 0099 ld $99 ;$1299 = 4761 = floor(138 ** 2 / 4)
|
|
018b 00de ld $de ;$12de = 4830 = floor(139 ** 2 / 4)
|
|
018c 0024 ld $24 ;$1324 = 4900 = floor(140 ** 2 / 4)
|
|
018d 006a ld $6a ;$136a = 4970 = floor(141 ** 2 / 4)
|
|
018e 00b1 ld $b1 ;$13b1 = 5041 = floor(142 ** 2 / 4)
|
|
018f 00f8 ld $f8 ;$13f8 = 5112 = floor(143 ** 2 / 4)
|
|
0190 0040 ld $40 ;$1440 = 5184 = floor(144 ** 2 / 4)
|
|
0191 0088 ld $88 ;$1488 = 5256 = floor(145 ** 2 / 4)
|
|
0192 00d1 ld $d1 ;$14d1 = 5329 = floor(146 ** 2 / 4)
|
|
0193 001a ld $1a ;$151a = 5402 = floor(147 ** 2 / 4)
|
|
0194 0064 ld $64 ;$1564 = 5476 = floor(148 ** 2 / 4)
|
|
0195 00ae ld $ae ;$15ae = 5550 = floor(149 ** 2 / 4)
|
|
0196 00f9 ld $f9 ;$15f9 = 5625 = floor(150 ** 2 / 4)
|
|
0197 0044 ld $44 ;$1644 = 5700 = floor(151 ** 2 / 4)
|
|
0198 0090 ld $90 ;$1690 = 5776 = floor(152 ** 2 / 4)
|
|
0199 00dc ld $dc ;$16dc = 5852 = floor(153 ** 2 / 4)
|
|
019a 0029 ld $29 ;$1729 = 5929 = floor(154 ** 2 / 4)
|
|
019b 0076 ld $76 ;$1776 = 6006 = floor(155 ** 2 / 4)
|
|
019c 00c4 ld $c4 ;$17c4 = 6084 = floor(156 ** 2 / 4)
|
|
019d 0012 ld $12 ;$1812 = 6162 = floor(157 ** 2 / 4)
|
|
019e 0061 ld $61 ;$1861 = 6241 = floor(158 ** 2 / 4)
|
|
019f 00b0 ld $b0 ;$18b0 = 6320 = floor(159 ** 2 / 4)
|
|
01a0 0000 ld $00 ;$1900 = 6400 = floor(160 ** 2 / 4)
|
|
01a1 0050 ld $50 ;$1950 = 6480 = floor(161 ** 2 / 4)
|
|
01a2 00a1 ld $a1 ;$19a1 = 6561 = floor(162 ** 2 / 4)
|
|
01a3 00f2 ld $f2 ;$19f2 = 6642 = floor(163 ** 2 / 4)
|
|
01a4 0044 ld $44 ;$1a44 = 6724 = floor(164 ** 2 / 4)
|
|
01a5 0096 ld $96 ;$1a96 = 6806 = floor(165 ** 2 / 4)
|
|
01a6 00e9 ld $e9 ;$1ae9 = 6889 = floor(166 ** 2 / 4)
|
|
01a7 003c ld $3c ;$1b3c = 6972 = floor(167 ** 2 / 4)
|
|
01a8 0090 ld $90 ;$1b90 = 7056 = floor(168 ** 2 / 4)
|
|
01a9 00e4 ld $e4 ;$1be4 = 7140 = floor(169 ** 2 / 4)
|
|
01aa 0039 ld $39 ;$1c39 = 7225 = floor(170 ** 2 / 4)
|
|
01ab 008e ld $8e ;$1c8e = 7310 = floor(171 ** 2 / 4)
|
|
01ac 00e4 ld $e4 ;$1ce4 = 7396 = floor(172 ** 2 / 4)
|
|
01ad 003a ld $3a ;$1d3a = 7482 = floor(173 ** 2 / 4)
|
|
01ae 0091 ld $91 ;$1d91 = 7569 = floor(174 ** 2 / 4)
|
|
01af 00e8 ld $e8 ;$1de8 = 7656 = floor(175 ** 2 / 4)
|
|
01b0 0040 ld $40 ;$1e40 = 7744 = floor(176 ** 2 / 4)
|
|
01b1 0098 ld $98 ;$1e98 = 7832 = floor(177 ** 2 / 4)
|
|
01b2 00f1 ld $f1 ;$1ef1 = 7921 = floor(178 ** 2 / 4)
|
|
01b3 004a ld $4a ;$1f4a = 8010 = floor(179 ** 2 / 4)
|
|
01b4 00a4 ld $a4 ;$1fa4 = 8100 = floor(180 ** 2 / 4)
|
|
01b5 00fe ld $fe ;$1ffe = 8190 = floor(181 ** 2 / 4)
|
|
01b6 0059 ld $59 ;$2059 = 8281 = floor(182 ** 2 / 4)
|
|
01b7 00b4 ld $b4 ;$20b4 = 8372 = floor(183 ** 2 / 4)
|
|
01b8 0010 ld $10 ;$2110 = 8464 = floor(184 ** 2 / 4)
|
|
01b9 006c ld $6c ;$216c = 8556 = floor(185 ** 2 / 4)
|
|
01ba 00c9 ld $c9 ;$21c9 = 8649 = floor(186 ** 2 / 4)
|
|
01bb 0026 ld $26 ;$2226 = 8742 = floor(187 ** 2 / 4)
|
|
01bc 0084 ld $84 ;$2284 = 8836 = floor(188 ** 2 / 4)
|
|
01bd 00e2 ld $e2 ;$22e2 = 8930 = floor(189 ** 2 / 4)
|
|
01be 0041 ld $41 ;$2341 = 9025 = floor(190 ** 2 / 4)
|
|
01bf 00a0 ld $a0 ;$23a0 = 9120 = floor(191 ** 2 / 4)
|
|
01c0 0000 ld $00 ;$2400 = 9216 = floor(192 ** 2 / 4)
|
|
01c1 0060 ld $60 ;$2460 = 9312 = floor(193 ** 2 / 4)
|
|
01c2 00c1 ld $c1 ;$24c1 = 9409 = floor(194 ** 2 / 4)
|
|
01c3 0022 ld $22 ;$2522 = 9506 = floor(195 ** 2 / 4)
|
|
01c4 0084 ld $84 ;$2584 = 9604 = floor(196 ** 2 / 4)
|
|
01c5 00e6 ld $e6 ;$25e6 = 9702 = floor(197 ** 2 / 4)
|
|
01c6 0049 ld $49 ;$2649 = 9801 = floor(198 ** 2 / 4)
|
|
01c7 00ac ld $ac ;$26ac = 9900 = floor(199 ** 2 / 4)
|
|
01c8 0010 ld $10 ;$2710 = 10000 = floor(200 ** 2 / 4)
|
|
01c9 0074 ld $74 ;$2774 = 10100 = floor(201 ** 2 / 4)
|
|
01ca 00d9 ld $d9 ;$27d9 = 10201 = floor(202 ** 2 / 4)
|
|
01cb 003e ld $3e ;$283e = 10302 = floor(203 ** 2 / 4)
|
|
01cc 00a4 ld $a4 ;$28a4 = 10404 = floor(204 ** 2 / 4)
|
|
01cd 000a ld $0a ;$290a = 10506 = floor(205 ** 2 / 4)
|
|
01ce 0071 ld $71 ;$2971 = 10609 = floor(206 ** 2 / 4)
|
|
01cf 00d8 ld $d8 ;$29d8 = 10712 = floor(207 ** 2 / 4)
|
|
01d0 0040 ld $40 ;$2a40 = 10816 = floor(208 ** 2 / 4)
|
|
01d1 00a8 ld $a8 ;$2aa8 = 10920 = floor(209 ** 2 / 4)
|
|
01d2 0011 ld $11 ;$2b11 = 11025 = floor(210 ** 2 / 4)
|
|
01d3 007a ld $7a ;$2b7a = 11130 = floor(211 ** 2 / 4)
|
|
01d4 00e4 ld $e4 ;$2be4 = 11236 = floor(212 ** 2 / 4)
|
|
01d5 004e ld $4e ;$2c4e = 11342 = floor(213 ** 2 / 4)
|
|
01d6 00b9 ld $b9 ;$2cb9 = 11449 = floor(214 ** 2 / 4)
|
|
01d7 0024 ld $24 ;$2d24 = 11556 = floor(215 ** 2 / 4)
|
|
01d8 0090 ld $90 ;$2d90 = 11664 = floor(216 ** 2 / 4)
|
|
01d9 00fc ld $fc ;$2dfc = 11772 = floor(217 ** 2 / 4)
|
|
01da 0069 ld $69 ;$2e69 = 11881 = floor(218 ** 2 / 4)
|
|
01db 00d6 ld $d6 ;$2ed6 = 11990 = floor(219 ** 2 / 4)
|
|
01dc 0044 ld $44 ;$2f44 = 12100 = floor(220 ** 2 / 4)
|
|
01dd 00b2 ld $b2 ;$2fb2 = 12210 = floor(221 ** 2 / 4)
|
|
01de 0021 ld $21 ;$3021 = 12321 = floor(222 ** 2 / 4)
|
|
01df 0090 ld $90 ;$3090 = 12432 = floor(223 ** 2 / 4)
|
|
01e0 0000 ld $00 ;$3100 = 12544 = floor(224 ** 2 / 4)
|
|
01e1 0070 ld $70 ;$3170 = 12656 = floor(225 ** 2 / 4)
|
|
01e2 00e1 ld $e1 ;$31e1 = 12769 = floor(226 ** 2 / 4)
|
|
01e3 0052 ld $52 ;$3252 = 12882 = floor(227 ** 2 / 4)
|
|
01e4 00c4 ld $c4 ;$32c4 = 12996 = floor(228 ** 2 / 4)
|
|
01e5 0036 ld $36 ;$3336 = 13110 = floor(229 ** 2 / 4)
|
|
01e6 00a9 ld $a9 ;$33a9 = 13225 = floor(230 ** 2 / 4)
|
|
01e7 001c ld $1c ;$341c = 13340 = floor(231 ** 2 / 4)
|
|
01e8 0090 ld $90 ;$3490 = 13456 = floor(232 ** 2 / 4)
|
|
01e9 0004 ld $04 ;$3504 = 13572 = floor(233 ** 2 / 4)
|
|
01ea 0079 ld $79 ;$3579 = 13689 = floor(234 ** 2 / 4)
|
|
01eb 00ee ld $ee ;$35ee = 13806 = floor(235 ** 2 / 4)
|
|
01ec 0064 ld $64 ;$3664 = 13924 = floor(236 ** 2 / 4)
|
|
01ed 00da ld $da ;$36da = 14042 = floor(237 ** 2 / 4)
|
|
01ee 0051 ld $51 ;$3751 = 14161 = floor(238 ** 2 / 4)
|
|
01ef 00c8 ld $c8 ;$37c8 = 14280 = floor(239 ** 2 / 4)
|
|
01f0 0040 ld $40 ;$3840 = 14400 = floor(240 ** 2 / 4)
|
|
01f1 00b8 ld $b8 ;$38b8 = 14520 = floor(241 ** 2 / 4)
|
|
01f2 0031 ld $31 ;$3931 = 14641 = floor(242 ** 2 / 4)
|
|
01f3 00aa ld $aa ;$39aa = 14762 = floor(243 ** 2 / 4)
|
|
01f4 0024 ld $24 ;$3a24 = 14884 = floor(244 ** 2 / 4)
|
|
01f5 009e ld $9e ;$3a9e = 15006 = floor(245 ** 2 / 4)
|
|
01f6 0019 ld $19 ;$3b19 = 15129 = floor(246 ** 2 / 4)
|
|
01f7 0094 ld $94 ;$3b94 = 15252 = floor(247 ** 2 / 4)
|
|
01f8 0010 ld $10 ;$3c10 = 15376 = floor(248 ** 2 / 4)
|
|
01f9 008c ld $8c ;$3c8c = 15500 = floor(249 ** 2 / 4)
|
|
01fa 0009 ld $09 ;$3d09 = 15625 = floor(250 ** 2 / 4)
|
|
01fb 0086 ld $86 ;$3d86 = 15750 = floor(251 ** 2 / 4)
|
|
01fc 0004 ld $04 ;$3e04 = 15876 = floor(252 ** 2 / 4)
|
|
01fd 0082 ld $82 ;$3e82 = 16002 = floor(253 ** 2 / 4)
|
|
01fe 0001 ld $01 ;$3f01 = 16129 = floor(254 ** 2 / 4)
|
|
01ff 0080 ld $80 ;$3f80 = 16256 = floor(255 ** 2 / 4)
|
|
128 C(f"${val:04x} = {val} = floor({i} ** 2 / 4)")
|
|
129
|
|
130
|
|
131 # Code copied from the main ROM. This provides a lookup table for right-shifts
|
|
132 align(0x100, size=0x100)
|
|
133 label("shiftTable")
|
|
134 shiftTable = pc()
|
|
135
|
|
136 for ix in range(255):
|
|
137 for n in range(1, 9): # Find first zero
|
|
138 if ~ix & (1 << (n - 1)):
|
|
139 break
|
|
140 pattern = ["x" if i < n else "1" if ix & (1 << i) else "0" for i in range(8)]
|
|
141 ld(ix >> n)
|
|
shiftTable: 0200 0000 ld $00 ;0b0000000x >> 1
|
|
0201 0000 ld $00 ;0b000000xx >> 2
|
|
0202 0001 ld $01 ;0b0000001x >> 1
|
|
0203 0000 ld $00 ;0b00000xxx >> 3
|
|
0204 0002 ld $02 ;0b0000010x >> 1
|
|
0205 0001 ld $01 ;0b000001xx >> 2
|
|
0206 0003 ld $03 ;0b0000011x >> 1
|
|
0207 0000 ld $00 ;0b0000xxxx >> 4
|
|
0208 0004 ld $04 ;0b0000100x >> 1
|
|
0209 0002 ld $02 ;0b000010xx >> 2
|
|
020a 0005 ld $05 ;0b0000101x >> 1
|
|
020b 0001 ld $01 ;0b00001xxx >> 3
|
|
020c 0006 ld $06 ;0b0000110x >> 1
|
|
020d 0003 ld $03 ;0b000011xx >> 2
|
|
020e 0007 ld $07 ;0b0000111x >> 1
|
|
020f 0000 ld $00 ;0b000xxxxx >> 5
|
|
0210 0008 ld $08 ;0b0001000x >> 1
|
|
0211 0004 ld $04 ;0b000100xx >> 2
|
|
0212 0009 ld $09 ;0b0001001x >> 1
|
|
0213 0002 ld $02 ;0b00010xxx >> 3
|
|
0214 000a ld $0a ;0b0001010x >> 1
|
|
0215 0005 ld $05 ;0b000101xx >> 2
|
|
0216 000b ld $0b ;0b0001011x >> 1
|
|
0217 0001 ld $01 ;0b0001xxxx >> 4
|
|
0218 000c ld $0c ;0b0001100x >> 1
|
|
0219 0006 ld $06 ;0b000110xx >> 2
|
|
021a 000d ld $0d ;0b0001101x >> 1
|
|
021b 0003 ld $03 ;0b00011xxx >> 3
|
|
021c 000e ld $0e ;0b0001110x >> 1
|
|
021d 0007 ld $07 ;0b000111xx >> 2
|
|
021e 000f ld $0f ;0b0001111x >> 1
|
|
021f 0000 ld $00 ;0b00xxxxxx >> 6
|
|
0220 0010 ld $10 ;0b0010000x >> 1
|
|
0221 0008 ld $08 ;0b001000xx >> 2
|
|
0222 0011 ld $11 ;0b0010001x >> 1
|
|
0223 0004 ld $04 ;0b00100xxx >> 3
|
|
0224 0012 ld $12 ;0b0010010x >> 1
|
|
0225 0009 ld $09 ;0b001001xx >> 2
|
|
0226 0013 ld $13 ;0b0010011x >> 1
|
|
0227 0002 ld $02 ;0b0010xxxx >> 4
|
|
0228 0014 ld $14 ;0b0010100x >> 1
|
|
0229 000a ld $0a ;0b001010xx >> 2
|
|
022a 0015 ld $15 ;0b0010101x >> 1
|
|
022b 0005 ld $05 ;0b00101xxx >> 3
|
|
022c 0016 ld $16 ;0b0010110x >> 1
|
|
022d 000b ld $0b ;0b001011xx >> 2
|
|
022e 0017 ld $17 ;0b0010111x >> 1
|
|
022f 0001 ld $01 ;0b001xxxxx >> 5
|
|
0230 0018 ld $18 ;0b0011000x >> 1
|
|
0231 000c ld $0c ;0b001100xx >> 2
|
|
0232 0019 ld $19 ;0b0011001x >> 1
|
|
0233 0006 ld $06 ;0b00110xxx >> 3
|
|
0234 001a ld $1a ;0b0011010x >> 1
|
|
0235 000d ld $0d ;0b001101xx >> 2
|
|
0236 001b ld $1b ;0b0011011x >> 1
|
|
0237 0003 ld $03 ;0b0011xxxx >> 4
|
|
0238 001c ld $1c ;0b0011100x >> 1
|
|
0239 000e ld $0e ;0b001110xx >> 2
|
|
023a 001d ld $1d ;0b0011101x >> 1
|
|
023b 0007 ld $07 ;0b00111xxx >> 3
|
|
023c 001e ld $1e ;0b0011110x >> 1
|
|
023d 000f ld $0f ;0b001111xx >> 2
|
|
023e 001f ld $1f ;0b0011111x >> 1
|
|
023f 0000 ld $00 ;0b0xxxxxxx >> 7
|
|
0240 0020 ld $20 ;0b0100000x >> 1
|
|
0241 0010 ld $10 ;0b010000xx >> 2
|
|
0242 0021 ld $21 ;0b0100001x >> 1
|
|
0243 0008 ld $08 ;0b01000xxx >> 3
|
|
0244 0022 ld $22 ;0b0100010x >> 1
|
|
0245 0011 ld $11 ;0b010001xx >> 2
|
|
0246 0023 ld $23 ;0b0100011x >> 1
|
|
0247 0004 ld $04 ;0b0100xxxx >> 4
|
|
0248 0024 ld $24 ;0b0100100x >> 1
|
|
0249 0012 ld $12 ;0b010010xx >> 2
|
|
024a 0025 ld $25 ;0b0100101x >> 1
|
|
024b 0009 ld $09 ;0b01001xxx >> 3
|
|
024c 0026 ld $26 ;0b0100110x >> 1
|
|
024d 0013 ld $13 ;0b010011xx >> 2
|
|
024e 0027 ld $27 ;0b0100111x >> 1
|
|
024f 0002 ld $02 ;0b010xxxxx >> 5
|
|
0250 0028 ld $28 ;0b0101000x >> 1
|
|
0251 0014 ld $14 ;0b010100xx >> 2
|
|
0252 0029 ld $29 ;0b0101001x >> 1
|
|
0253 000a ld $0a ;0b01010xxx >> 3
|
|
0254 002a ld $2a ;0b0101010x >> 1
|
|
0255 0015 ld $15 ;0b010101xx >> 2
|
|
0256 002b ld $2b ;0b0101011x >> 1
|
|
0257 0005 ld $05 ;0b0101xxxx >> 4
|
|
0258 002c ld $2c ;0b0101100x >> 1
|
|
0259 0016 ld $16 ;0b010110xx >> 2
|
|
025a 002d ld $2d ;0b0101101x >> 1
|
|
025b 000b ld $0b ;0b01011xxx >> 3
|
|
025c 002e ld $2e ;0b0101110x >> 1
|
|
025d 0017 ld $17 ;0b010111xx >> 2
|
|
025e 002f ld $2f ;0b0101111x >> 1
|
|
025f 0001 ld $01 ;0b01xxxxxx >> 6
|
|
0260 0030 ld $30 ;0b0110000x >> 1
|
|
0261 0018 ld $18 ;0b011000xx >> 2
|
|
0262 0031 ld $31 ;0b0110001x >> 1
|
|
0263 000c ld $0c ;0b01100xxx >> 3
|
|
0264 0032 ld $32 ;0b0110010x >> 1
|
|
0265 0019 ld $19 ;0b011001xx >> 2
|
|
0266 0033 ld $33 ;0b0110011x >> 1
|
|
0267 0006 ld $06 ;0b0110xxxx >> 4
|
|
0268 0034 ld $34 ;0b0110100x >> 1
|
|
0269 001a ld $1a ;0b011010xx >> 2
|
|
026a 0035 ld $35 ;0b0110101x >> 1
|
|
026b 000d ld $0d ;0b01101xxx >> 3
|
|
026c 0036 ld $36 ;0b0110110x >> 1
|
|
026d 001b ld $1b ;0b011011xx >> 2
|
|
026e 0037 ld $37 ;0b0110111x >> 1
|
|
026f 0003 ld $03 ;0b011xxxxx >> 5
|
|
0270 0038 ld $38 ;0b0111000x >> 1
|
|
0271 001c ld $1c ;0b011100xx >> 2
|
|
0272 0039 ld $39 ;0b0111001x >> 1
|
|
0273 000e ld $0e ;0b01110xxx >> 3
|
|
0274 003a ld $3a ;0b0111010x >> 1
|
|
0275 001d ld $1d ;0b011101xx >> 2
|
|
0276 003b ld $3b ;0b0111011x >> 1
|
|
0277 0007 ld $07 ;0b0111xxxx >> 4
|
|
0278 003c ld $3c ;0b0111100x >> 1
|
|
0279 001e ld $1e ;0b011110xx >> 2
|
|
027a 003d ld $3d ;0b0111101x >> 1
|
|
027b 000f ld $0f ;0b01111xxx >> 3
|
|
027c 003e ld $3e ;0b0111110x >> 1
|
|
027d 001f ld $1f ;0b011111xx >> 2
|
|
027e 003f ld $3f ;0b0111111x >> 1
|
|
027f 0000 ld $00 ;0bxxxxxxxx >> 8
|
|
0280 0040 ld $40 ;0b1000000x >> 1
|
|
0281 0020 ld $20 ;0b100000xx >> 2
|
|
0282 0041 ld $41 ;0b1000001x >> 1
|
|
0283 0010 ld $10 ;0b10000xxx >> 3
|
|
0284 0042 ld $42 ;0b1000010x >> 1
|
|
0285 0021 ld $21 ;0b100001xx >> 2
|
|
0286 0043 ld $43 ;0b1000011x >> 1
|
|
0287 0008 ld $08 ;0b1000xxxx >> 4
|
|
0288 0044 ld $44 ;0b1000100x >> 1
|
|
0289 0022 ld $22 ;0b100010xx >> 2
|
|
028a 0045 ld $45 ;0b1000101x >> 1
|
|
028b 0011 ld $11 ;0b10001xxx >> 3
|
|
028c 0046 ld $46 ;0b1000110x >> 1
|
|
028d 0023 ld $23 ;0b100011xx >> 2
|
|
028e 0047 ld $47 ;0b1000111x >> 1
|
|
028f 0004 ld $04 ;0b100xxxxx >> 5
|
|
0290 0048 ld $48 ;0b1001000x >> 1
|
|
0291 0024 ld $24 ;0b100100xx >> 2
|
|
0292 0049 ld $49 ;0b1001001x >> 1
|
|
0293 0012 ld $12 ;0b10010xxx >> 3
|
|
0294 004a ld $4a ;0b1001010x >> 1
|
|
0295 0025 ld $25 ;0b100101xx >> 2
|
|
0296 004b ld $4b ;0b1001011x >> 1
|
|
0297 0009 ld $09 ;0b1001xxxx >> 4
|
|
0298 004c ld $4c ;0b1001100x >> 1
|
|
0299 0026 ld $26 ;0b100110xx >> 2
|
|
029a 004d ld $4d ;0b1001101x >> 1
|
|
029b 0013 ld $13 ;0b10011xxx >> 3
|
|
029c 004e ld $4e ;0b1001110x >> 1
|
|
029d 0027 ld $27 ;0b100111xx >> 2
|
|
029e 004f ld $4f ;0b1001111x >> 1
|
|
029f 0002 ld $02 ;0b10xxxxxx >> 6
|
|
02a0 0050 ld $50 ;0b1010000x >> 1
|
|
02a1 0028 ld $28 ;0b101000xx >> 2
|
|
02a2 0051 ld $51 ;0b1010001x >> 1
|
|
02a3 0014 ld $14 ;0b10100xxx >> 3
|
|
02a4 0052 ld $52 ;0b1010010x >> 1
|
|
02a5 0029 ld $29 ;0b101001xx >> 2
|
|
02a6 0053 ld $53 ;0b1010011x >> 1
|
|
02a7 000a ld $0a ;0b1010xxxx >> 4
|
|
02a8 0054 ld $54 ;0b1010100x >> 1
|
|
02a9 002a ld $2a ;0b101010xx >> 2
|
|
02aa 0055 ld $55 ;0b1010101x >> 1
|
|
02ab 0015 ld $15 ;0b10101xxx >> 3
|
|
02ac 0056 ld $56 ;0b1010110x >> 1
|
|
02ad 002b ld $2b ;0b101011xx >> 2
|
|
02ae 0057 ld $57 ;0b1010111x >> 1
|
|
02af 0005 ld $05 ;0b101xxxxx >> 5
|
|
02b0 0058 ld $58 ;0b1011000x >> 1
|
|
02b1 002c ld $2c ;0b101100xx >> 2
|
|
02b2 0059 ld $59 ;0b1011001x >> 1
|
|
02b3 0016 ld $16 ;0b10110xxx >> 3
|
|
02b4 005a ld $5a ;0b1011010x >> 1
|
|
02b5 002d ld $2d ;0b101101xx >> 2
|
|
02b6 005b ld $5b ;0b1011011x >> 1
|
|
02b7 000b ld $0b ;0b1011xxxx >> 4
|
|
02b8 005c ld $5c ;0b1011100x >> 1
|
|
02b9 002e ld $2e ;0b101110xx >> 2
|
|
02ba 005d ld $5d ;0b1011101x >> 1
|
|
02bb 0017 ld $17 ;0b10111xxx >> 3
|
|
02bc 005e ld $5e ;0b1011110x >> 1
|
|
02bd 002f ld $2f ;0b101111xx >> 2
|
|
02be 005f ld $5f ;0b1011111x >> 1
|
|
02bf 0001 ld $01 ;0b1xxxxxxx >> 7
|
|
02c0 0060 ld $60 ;0b1100000x >> 1
|
|
02c1 0030 ld $30 ;0b110000xx >> 2
|
|
02c2 0061 ld $61 ;0b1100001x >> 1
|
|
02c3 0018 ld $18 ;0b11000xxx >> 3
|
|
02c4 0062 ld $62 ;0b1100010x >> 1
|
|
02c5 0031 ld $31 ;0b110001xx >> 2
|
|
02c6 0063 ld $63 ;0b1100011x >> 1
|
|
02c7 000c ld $0c ;0b1100xxxx >> 4
|
|
02c8 0064 ld $64 ;0b1100100x >> 1
|
|
02c9 0032 ld $32 ;0b110010xx >> 2
|
|
02ca 0065 ld $65 ;0b1100101x >> 1
|
|
02cb 0019 ld $19 ;0b11001xxx >> 3
|
|
02cc 0066 ld $66 ;0b1100110x >> 1
|
|
02cd 0033 ld $33 ;0b110011xx >> 2
|
|
02ce 0067 ld $67 ;0b1100111x >> 1
|
|
02cf 0006 ld $06 ;0b110xxxxx >> 5
|
|
02d0 0068 ld $68 ;0b1101000x >> 1
|
|
02d1 0034 ld $34 ;0b110100xx >> 2
|
|
02d2 0069 ld $69 ;0b1101001x >> 1
|
|
02d3 001a ld $1a ;0b11010xxx >> 3
|
|
02d4 006a ld $6a ;0b1101010x >> 1
|
|
02d5 0035 ld $35 ;0b110101xx >> 2
|
|
02d6 006b ld $6b ;0b1101011x >> 1
|
|
02d7 000d ld $0d ;0b1101xxxx >> 4
|
|
02d8 006c ld $6c ;0b1101100x >> 1
|
|
02d9 0036 ld $36 ;0b110110xx >> 2
|
|
02da 006d ld $6d ;0b1101101x >> 1
|
|
02db 001b ld $1b ;0b11011xxx >> 3
|
|
02dc 006e ld $6e ;0b1101110x >> 1
|
|
02dd 0037 ld $37 ;0b110111xx >> 2
|
|
02de 006f ld $6f ;0b1101111x >> 1
|
|
02df 0003 ld $03 ;0b11xxxxxx >> 6
|
|
02e0 0070 ld $70 ;0b1110000x >> 1
|
|
02e1 0038 ld $38 ;0b111000xx >> 2
|
|
02e2 0071 ld $71 ;0b1110001x >> 1
|
|
02e3 001c ld $1c ;0b11100xxx >> 3
|
|
02e4 0072 ld $72 ;0b1110010x >> 1
|
|
02e5 0039 ld $39 ;0b111001xx >> 2
|
|
02e6 0073 ld $73 ;0b1110011x >> 1
|
|
02e7 000e ld $0e ;0b1110xxxx >> 4
|
|
02e8 0074 ld $74 ;0b1110100x >> 1
|
|
02e9 003a ld $3a ;0b111010xx >> 2
|
|
02ea 0075 ld $75 ;0b1110101x >> 1
|
|
02eb 001d ld $1d ;0b11101xxx >> 3
|
|
02ec 0076 ld $76 ;0b1110110x >> 1
|
|
02ed 003b ld $3b ;0b111011xx >> 2
|
|
02ee 0077 ld $77 ;0b1110111x >> 1
|
|
02ef 0007 ld $07 ;0b111xxxxx >> 5
|
|
02f0 0078 ld $78 ;0b1111000x >> 1
|
|
02f1 003c ld $3c ;0b111100xx >> 2
|
|
02f2 0079 ld $79 ;0b1111001x >> 1
|
|
02f3 001e ld $1e ;0b11110xxx >> 3
|
|
02f4 007a ld $7a ;0b1111010x >> 1
|
|
02f5 003d ld $3d ;0b111101xx >> 2
|
|
02f6 007b ld $7b ;0b1111011x >> 1
|
|
02f7 000f ld $0f ;0b1111xxxx >> 4
|
|
02f8 007c ld $7c ;0b1111100x >> 1
|
|
02f9 003e ld $3e ;0b111110xx >> 2
|
|
02fa 007d ld $7d ;0b1111101x >> 1
|
|
02fb 001f ld $1f ;0b11111xxx >> 3
|
|
02fc 007e ld $7e ;0b1111110x >> 1
|
|
02fd 003f ld $3f ;0b111111xx >> 2
|
|
02fe 007f ld $7f ;0b1111111x >> 1
|
|
142 C("0b%s >> %d" % ("".join(reversed(pattern)), n))
|
|
143
|
|
144 assert pc() & 255 == 255
|
|
02ff fd06 bra [$06] 145 bra([continuation]) # Jumps back into next page
|
|
146 align(0x100, size=0x100)
|
|
0300 0200 nop 147 nop() #
|
|
148
|
|
149 label("multiply 7x7")
|
|
150 # The formula is floor(((a + b) ** 2) / 4) - floor(((a - b) ** 2) / 4)
|
|
151
|
|
multiply 7x7: 0301 0009 ld $09 152 ld(".after-first-lookup") # 1
|
|
0302 c206 st [$06] 153 st([continuation])
|
|
0303 1400 ld $00,y 154 ld(hi("Quarter-squares lookup table"), Y)
|
|
0304 00fd ld $fd 155 ld("high-byte action.store")
|
|
0305 c207 st [$07] 156 st([high_byte_action]) # 5
|
|
0306 0101 ld [$01] 157 ld([a])
|
|
0307 e0e8 jmp y,$e8 158 jmp(Y, "table entry") # 7
|
|
0308 8102 adda [$02] 159 adda([b]) # 8
|
|
160 cost_to_first_lookup = 8
|
|
161
|
|
162 cost_after_first_lookup = (
|
|
163 cost_to_first_lookup
|
|
164 + cost_of_high_byte_table_entry
|
|
165 + cost_of_high_byte_store
|
|
166 + cost_of_low_byte_table_entry
|
|
167 + cost_of_low_byte_return
|
|
168 )
|
|
169
|
|
170 label(".after-first-lookup")
|
|
171 # On return we have the low-byte in the accumulator
|
|
172 # We can safely add one to it without causing an overflow,
|
|
173 # because 255 does not appear in the low-byte table.
|
|
174 # This is part of the following subtraction.
|
|
.after-first-lookup:
|
|
0309 8001 adda $01 175 adda(1) # 1
|
|
030a c203 st [$03] 176 st([result])
|
|
030b 0012 ld $12 177 ld(".after-second-lookup")
|
|
030c c206 st [$06] 178 st([continuation])
|
|
030d 00fb ld $fb 179 ld("high-byte action.invert-and-add") # 5
|
|
030e c207 st [$07] 180 st([high_byte_action])
|
|
030f 0101 ld [$01] 181 ld([a])
|
|
0310 e0e3 jmp y,$e3 182 jmp(Y, "table entry.possibly-negative") # 8
|
|
0311 a102 suba [$02] 183 suba([b]) # 9
|
|
184 cost_between_lookups = 9
|
|
185
|
|
186 cost_after_second_lookup = (
|
|
187 cost_after_first_lookup
|
|
188 + cost_between_lookups
|
|
189 + cost_of_absolute
|
|
190 + cost_of_high_byte_table_entry
|
|
191 + cost_of_high_byte_invert_and_add
|
|
192 + cost_of_low_byte_table_entry
|
|
193 + cost_of_low_byte_return
|
|
194 )
|
|
195
|
|
196 label(".after-second-lookup")
|
|
.after-second-lookup:
|
|
0312 60ff xora $ff 197 xora(0xFF) # 1
|
|
198 # We need to add this to the result
|
|
199 # But we may have a carry
|
|
0313 8103 adda [$03] 200 adda([result])
|
|
0314 c205 st [$05] 201 st([tmp])
|
|
0315 e819 blt $0319 202 blt(pc() + 4) # 5
|
|
0316 a103 suba [$03] 203 suba([result]) # 6
|
|
0317 fc1b bra $031b 204 bra(pc() + 4) # 7
|
|
0318 4103 ora [$03] 205 ora([result]) # 8
|
|
0319 fc1b bra $031b 206 bra(pc() + 2) # 7
|
|
031a 2103 anda [$03] 207 anda([result]) # 8
|
|
031b 3080 anda $80,x 208 anda(0b1000_0000, X) # 9
|
|
031c 0105 ld [$05] 209 ld([tmp])
|
|
031d c203 st [$03] 210 st([result]) # 10
|
|
031e 0500 ld [x] 211 ld([X])
|
|
031f 8104 adda [$04] 212 adda([result + 1])
|
|
213 st([result + 1]) # 13
|
|
0320 c204 st [$04] ;Total cost: 64 cycles
|
|
214 cost_of_final_add = 13
|
|
215
|
|
216 cost_of_7bit_multiply = cost_after_second_lookup + cost_of_final_add
|
|
217 C(f"Total cost: {cost_of_7bit_multiply} cycles")
|
|
218 label("done")
|
|
done: 0321 0200 nop 219 nop()
|
|
220
|
|
221
|
|
222 label("multiply 8x8")
|
|
223 # Extend the 7bit x 7bit multiplication to 8bit x 8bit
|
|
224 #
|
|
225 # The logic goes as follows.
|
|
226 # Let A and B be the low seven bits of a and b, e.g.
|
|
227 # A = a₆2⁶ + a₅2⁵ + a₄2⁴ + a₃2³ + a₂2² + a₁2¹ + a₀2⁰
|
|
228 # B = b₆2⁶ + b₅2⁵ + b₄2⁴ + b₃2³ + b₂2² + b₁2¹ + b₀2⁰
|
|
229 # Then we could think of 8bit x 8bit multiply as
|
|
230 # (a₇2⁷ + A)(b₇2⁷ + B)
|
|
231 # Multiplying out the brackets gives
|
|
232 # a₇2⁷b₇2⁷ + a₇2⁷B + b₇2⁷A + AB
|
|
233 # (and AB is the result we already know how to calculate)
|
|
234 # Simplifying a bit, we get
|
|
235 # a₇b₇2¹⁴ + 2⁷(a₇B + b₇A) + AB
|
|
236 # Since a₇ and b₇ are one or zero, multiplying by them is like an if.
|
|
237 # We can consider four cases,
|
|
238 # if a₇ is 1 and b₇ is 1:
|
|
239 # 2¹⁴ + 2⁷(B + A) + AB
|
|
240 # if a₇ is 1 and b₇ is 0:
|
|
241 # 2⁷B + AB
|
|
242 # if a₇ is 0 and b₇ is 1:
|
|
243 # 2⁷A + AB
|
|
244 # if a₇ is 0 and b₇ is 0:
|
|
245 # AB
|
|
246 # Multiplication by 2⁷(=128) is a left-shift by seven, which
|
|
247 # can be broken down as moving the LSB of the low-byte to the
|
|
248 # MSB of the low-byte, and setting all of the other bits to zero,
|
|
249 # The other bits of the low byte can be right-shifted by one,
|
|
250 # and moved to the high byte.
|
|
251
|
|
252 # Test which of the branches we are on.
|
|
multiply 8x8: 0322 0101 ld [$01] 253 ld([a]) # 1
|
|
0323 6102 xora [$02] 254 xora([b])
|
|
255 blt(".one MSB set") # 3
|
|
0324 e83b blt .one MSB set
|
|
0325 0101 ld [$01] 256 ld([a]) # 4
|
|
257
|
|
258 # Both MSBs equal
|
|
259 bge("multiply 7x7") # 5
|
|
0326 f401 bge multiply 7x7
|
|
0327 207f anda $7f 260 anda(0b0111_1111) # 6
|
|
261 cost_of_8bit_multiply__both_msbs_low = cost_of_7bit_multiply + 6
|
|
262
|
|
263 # Both MSBs set
|
|
0328 c201 st [$01] 264 st([a]) # 7; a = A
|
|
0329 0049 ld $49 265 ld(".after right-shift")
|
|
032a c206 st [$06] 266 st([continuation])
|
|
032b 0040 ld $40 267 ld(2**14 >> 8) # 10; Write the high-byte for later addition.
|
|
032c c204 st [$04] 268 st([result + 1])
|
|
032d 0102 ld [$02] 269 ld([b])
|
|
032e 207f anda $7f 270 anda(0b0111_1111)
|
|
032f c202 st [$02] 271 st([b]) # b = B
|
|
0330 8101 adda [$01] 272 adda([a]) # 15
|
|
273 cost_of_both_msbs_set = 15
|
|
274
|
|
275 label(". << 7")
|
|
. << 7: 0331 c205 st [$05] 276 st([tmp]) # 1
|
|
0332 2001 anda $01 277 anda(0b0000_0001) # Clear all but the bottom bit
|
|
0333 807f adda $7f 278 adda(0b0111_1111) # Carries bottom bit to top bit
|
|
0334 2080 anda $80 279 anda(0b1000_0000) # Clears all but the top bit
|
|
0335 c203 st [$03] 280 st([result]) # 5
|
|
0336 0105 ld [$05] 281 ld([tmp])
|
|
0337 20fe anda $fe 282 anda(0b1111_1110) # Calculate index to right-shift-table
|
|
0338 1402 ld $02,y 283 ld(hi("shiftTable"), Y)
|
|
0339 e200 jmp y,ac 284 jmp(Y, AC) # 9
|
|
033a fcff bra $ff 285 bra(0xFF) # 10
|
|
286 # 11 ld (a + b) >> 1
|
|
287 # 12 bra [continuation]
|
|
288 # 12 nop
|
|
289 cost_of_right_shift = 13
|
|
290 cost_after_right_shift = cost_of_both_msbs_set + cost_of_right_shift
|
|
291
|
|
292 label(".one MSB set")
|
|
293 bge(".b has msb set") # 5
|
|
.one MSB set: 033b f443 bge .b has msb set
|
|
033c 007f ld $7f 294 ld(0b0111_1111) # 6
|
|
295 # a has msb set
|
|
033d 2101 anda [$01] 296 anda([a]) # 7
|
|
033e c201 st [$01] 297 st([a])
|
|
033f 004a ld $4a 298 ld(lo(".after right-shift") + 1)
|
|
0340 c206 st [$06] 299 st([continuation]) # 10
|
|
0341 fc31 bra . << 7 300 bra(". << 7") # 11
|
|
0342 0102 ld [$02] 301 ld([b]) # 12
|
|
302 label(".b has msb set")
|
|
.b has msb set:
|
|
0343 2102 anda [$02] 303 anda([b]) # 7
|
|
0344 c202 st [$02] 304 st([b])
|
|
0345 004a ld $4a 305 ld(lo(".after right-shift") + 1)
|
|
0346 c206 st [$06] 306 st([continuation]) # 10
|
|
0347 fc31 bra . << 7 307 bra(". << 7") # 11
|
|
0348 0101 ld [$01] 308 ld([a]) # 12
|
|
309 cost_of_one_msb_set = 12
|
|
310 one_msb_cost_saving = cost_of_both_msbs_set - cost_of_one_msb_set + 1
|
|
311
|
|
312 label(".after right-shift")
|
|
.after right-shift:
|
|
0349 8104 adda [$04] 313 adda([result + 1]) # 1
|
|
034a c204 st [$04] 314 st([result + 1])
|
|
034b 0053 ld $53 315 ld(".after-first-lookup-8bit")
|
|
034c c206 st [$06] 316 st([continuation])
|
|
034d 1400 ld $00,y 317 ld(hi("Quarter-squares lookup table"), Y) # 5
|
|
034e 00fc ld $fc 318 ld("high-byte action.add")
|
|
034f c207 st [$07] 319 st([high_byte_action])
|
|
0350 0101 ld [$01] 320 ld([a])
|
|
0351 e0e8 jmp y,$e8 321 jmp(Y, "table entry") # 9
|
|
0352 8102 adda [$02] 322 adda([b]) # 10
|
|
323 cost_of_after_right_shift = 10
|
|
324 cost_after_first_lookup__8bit = (
|
|
325 cost_after_right_shift
|
|
326 + cost_of_after_right_shift
|
|
327 + cost_of_high_byte_table_entry
|
|
328 + cost_of_high_byte_add
|
|
329 + cost_of_low_byte_table_entry
|
|
330 + cost_of_low_byte_return
|
|
331 )
|
|
332
|
|
333 label(".after-first-lookup-8bit")
|
|
334 # On return we have the low-byte in the accumulator
|
|
335 # We already either have 0 or 128 in the low byte of the result.
|
|
336 # Adding will cause an overflow into the high byte iff both
|
|
337 # have the MSB set, in which case the result will definitely
|
|
338 # not have the MSB set.
|
|
.after-first-lookup-8bit:
|
|
0353 8103 adda [$03] 339 adda([result]) # 1
|
|
0354 e857 blt $0357 340 blt(pc() + 3) # 2
|
|
0355 fc58 bra $0358 341 bra(pc() + 3) # 3
|
|
0356 1103 ld [$03],x 342 ld([result], X) # 4; May be a carry
|
|
0357 1000 ld $00,x 343 ld(0, X) # 4; Definitely no carry
|
|
344 # We can safely add one to the result without causing a further overflow,
|
|
345 # because 255 does not appear in the low-byte table.
|
|
346 # This is part of the subtraction.
|
|
0358 8001 adda $01 347 adda(1) # 5
|
|
0359 c203 st [$03] 348 st([result])
|
|
349 # Do the carry
|
|
035a 0500 ld [x] 350 ld([X])
|
|
035b 8104 adda [$04] 351 adda([result + 1])
|
|
035c c204 st [$04] 352 st([result + 1])
|
|
035d 0012 ld $12 353 ld(".after-second-lookup") # 10
|
|
035e c206 st [$06] 354 st([continuation])
|
|
035f 00fb ld $fb 355 ld("high-byte action.invert-and-add")
|
|
0360 c207 st [$07] 356 st([high_byte_action])
|
|
0361 0101 ld [$01] 357 ld([a])
|
|
0362 e0e3 jmp y,$e3 358 jmp(Y, "table entry.possibly-negative") # 15
|
|
0363 a102 suba [$02] ;Worst case cost: 102
|
|
;Saving when both values < 128: 32
|
|
;Saving when only one value < 128: 4
|
|
359 suba([b]) # 16
|
|
360 cost_between_lookups__8bit = 16
|
|
361
|
|
362 cost_after_second_lookup__8bit = (
|
|
363 cost_after_first_lookup__8bit
|
|
364 + cost_between_lookups__8bit
|
|
365 + cost_of_absolute
|
|
366 + cost_of_high_byte_table_entry
|
|
367 + cost_of_high_byte_invert_and_add
|
|
368 + cost_of_low_byte_table_entry
|
|
369 + cost_of_low_byte_return
|
|
370 )
|
|
371
|
|
372 cost_of_8bit_multiply = cost_after_second_lookup__8bit + cost_of_final_add
|
|
373 no_msb_cost_saving = cost_of_8bit_multiply - cost_of_8bit_multiply__both_msbs_low
|
|
374 C(f"Worst case cost: {cost_of_8bit_multiply}")
|
|
375 C(f"Saving when both values < 128: {no_msb_cost_saving}")
|
|
376 C(f"Saving when only one value < 128: {one_msb_cost_saving}")
|
|
377 end()
|
|
378
|
|
379 if __name__ == "__main__":
|
|
380 disableListing()
|
|
0364
|