Signetics 2650 & 2636 programming/BCD arithmetic
Binary Coded Decimal
editThe natural way for a microprocessor to represent numbers and perform arithmetic is in binary format. Humans prefer decimal notation, so when a computer wants to communicate with us, it has to convert from one format to the other. Another option is to force the computer to operate on binary-coded decimals, where each decimal digit, 0-9, is represented by 4-bits. The other six numbers that can be represented by those four bits, A16 - F16 are meaningless, and this makes binary arithmetic more complicated than in a pure binary format. If for example we do the addition 8 + 4 in binary, the result is C16 which is meaningless in BCD. To get the correct answer we must add six, giving 1216. The '1' is carried to the next most significant digit.
The primary use of BCD in these video game consoles is for displaying the score, a number that is typically only incremented, so our discussion here will be confined to addition of BCD numbers. Should your game require subtraction, signed integer arithmetic or division, the Application Memo listed in the references below should help.
Decimal adjust
editThe 2650 microprocessor has two features that aid BCD arithmetic. In addition to the Carry Flag (C) that registers a carry from bit 7, there is an Inter Digit Flag (IDC) that registers a carry from bit 3. There is also a special instruction, Decimal Adjust Register, DAR, that examines these flags after an arithmetic operation and makes appropriate adjustments to the high nibble, bits 4-7, and the low nibble, bits 0-3. In the case of the most significant nibble, it adds 1010 if C is 0, and for the least significant nibble it adds 1010 if IDC is 0.
Incrementing the score
editThe program listed below adds an 8-bit number to a 16-bit number and will probably take care of most score-handling needs on these consoles. The MSB of the score is saved in 1F50 and the LSB of the score is in 1F51. Don't forget that the score registers, 1FC8 and 1FC9, are write-only, so the current score cannot be retrieved from them, it must be saved elsewhere.
The WinArcadia debugger can be used to step through this program to see how it works. The incremental value and the initial 16-bit value need to be set using the Memory Editor.
0000 1F0004 : 0003 17 : 0004 7620 : 0006 7708 : 0008 7501 : 000A 0C1F51 : 000D 0D1F50 : 0010 0F1F52 : 0013 8466 : 0015 8566 : 0017 7501 : 0019 83 : 001A 94 : 001B 8500 : 001D 95 : 001E CC1F51 : 0021 CD1F50 : 0024 1B60 : : : : : : : : |
bcta,un start ; reset vector retc,un ; interrupt vector start: ppsu intinhibit ;inhibit interrupts loop: ppsl withcarry cpsl carry loda,r0 lsbA ; get the score loda,r1 msbA loda,r3 inc ; get the increment addi,r0 H'66' addi,r1 H'66' cpsl H'01' ; clear carry addz r3 ; perform BCD addition, scoreX = scoreX + r3 dar,r0 addi,r1 H'00' dar,r1 stra,r0 lsbA ; save result stra,r1 msbA bctr,un loop msbA equ $1F50 lsbA equ $1F51 inc equ $1F52 carry equ $01 withcarry equ $08 intinhibit equ $20 |
References
edit