I kinda like breaking down this project into multiple "lessons" as it were. It makes me feel like I'm following along lesson plans in a book. Perhaps I should go the other way around and actually make the book I would be following if I were following a book to make this thing.
The code for this can be found in my github repository.
I broke down the application into a few main steps:
- display the last result
- add together the two previous results
- store that sum into a result variable
When broken down further, we see that we also have to have some method for "kickstarting" it, as it were, since the first two numbers in the sequence do not follow the standard fibonacci sequence. (quick reminder: each value in the fibonacci sequence is the previous two values added together. very simple. So, for the first two values, there is no "previous two" so they are just hardcoded as "0, 1"
Computation of the sequence can be described as :
- hardcoded "0"
- hardcoded "1"
- use algorithm to sum previous two values
- same as 3
For doing the math, I wanted to have variables that mimiced the 3 bytes we are able to display on the KIM, so I use 24 bits (3 bytes) to store them. I broke down the math functions to be generic in that they can perform using two variables ("i" and "j") and store their result in a third variable "RESULT". From there, additional functions were created to move the values around between them. For example, we need to "roll" the values through if we want to make this repeatable. So the computation sequence can be described as:
- RESULT, I and J all set to '0'
- refresh display RESULT "0"
- RESULT gets "1"
- refresh display RESULT "1"
- shift the values through:
- J gets I's value
- I gets RESULT's value
- add: RESULT gets the value from adding I and J
- repeat at step "4"
And this is basically the procedure as seen in the source.
The multibyte addition was actually a lot simpler than I thought it was going to be. My first thought was "how could this possibly work if i were to add like 100 to 100... you end up with "2" for the carry instead of "1"." Obviously, you can see the error here, but for some reason this got stuck in my head and suddenly, all of the multibyte (16+ bit) math seemed near impossible to deal with. I think it was the multiplication that seemed hard, but when you break it down as multistep additions instead of multiplications, it all makes sense. I blame this on the cold and fuzzy head I have right now. I'm just not thinking right... also extra time at work... sure... and um... an ARP storm. all contributing factors to not thinking clearly. ;)
The basic procedure for doing multibyte math is to observe the carry bit. The carry bit is set when math on two 8 bit values exceeds the 8 bit container. If you think of it in decimal, when you add 1 to 9, you get "0" with a carry of "1" which ends up in the next digit space, resulting in a "10". So if you were to add 99 and 04, you end up with 03 with a carry of 1, resulting in "103". Math on the 6502 is no different, other than we're (probably) using hex where the value can go from 0-9,a-f rather than just 0-9 for each digit. The math for addition is basically:
- for each byte (starting from the least significant on the right)
- add one byte to the other, with the carry bit from the previous byte
- store that result in the RESULT
Or, more precisely
- clear "Carry" (Carry = 0)
- register A gets I0 (A = J0)
- add j0 to A. (A = A + J0 + Carry)
- store the result in RESULT0
- A = I1
- A = A + J1 + Carry
- RESULT1 = A
- A = I2
- A = A + J2 + Carry
- RESULT2 = A
I think you can see that this can be carried out indefinitely for multiple bytes.
The "display the result" was pretty straightforward as well. The "RESULT" bytes were stored into INH, POINTL and POINTH, and then the SCANDS function is called. This refreshes those three values out to the KIM's LED display. Then a call to GETKEY stores the current key press value into the accumulator register. If nothing is pressed, this fills A with $15, or KEY_NONE as I have it defined. Then it just sits in a tight loop refreshing the display and waiting for any key to be pressed.
- refresh display
- check for key press
- no key press? repeat at 1
So the end result is a program that advances to the next sequence number each time you press a key.
When it fills all the digits, when we get a "carry" on the third digit while doing the math, i display "EEEEEE" as a cheesy error display and wait for a press. When something is pressed then, it resets and starts all over agian.
As for BCD, I basically have run the code both in BCD (decimal) mode and hex mode, just to see how it works out. Turns out i was worried for nothing, It all 'just worked' fine in both modes.
So yeah. My throat is sore, and I'd love to just go to sleep right now.
Post a Comment