|Lesson A1 -Binary Coded Decimal|
|Lesson A2 - Interrupt Mode 2|
| Lesson A1 - Binary Coded Decimal
We've looked at HEX, and DECIMAL before, but there's one 'weird' way of storing data that - while not as efficient with memory - will let us store super-large numbers, and show them easily to the screen!... and that's Binary Coded Decimal!
|Binary Coded Decimal is Super-Simple... rather than storing 0-255 in a byte... we just store 0-9! Why do we want to do that? well converting numbers stored in HEX to ascii for the screen can be a real pain! but Binary Coded Decimal makes it easy... lets take a look!|
|Binary Coded Decimal||&01 &02 &03 &04|
|Packed Binary Coded Decimal||&12 &34|
|In the example above, the most significant byte was stored first, so 1345678 would be stored &12 &34 &56 &78... in this lessons examples we're going to store things backwards, so it would be stored &78 &56 &34 &12.... this is because many of our commands will start from the least significant byte, so we can save a little time this way!... if you don't like it, you can always change the code!|
|Some of our commands WILL need to start from the end of the
data... so we're going to create a simple function to alter HL and
DE to move to the end of a BCD number B bytes in length...
Our BCD commands are going to store the number of bytes in B... so to get HL and DE to point to the last byte, we need to add B-1
We're doing this to HL and DE because most of our commands use two parameters
|Showing BCD is easy!
First we need to move to the highest value byte... then we convert the top nibble to Ascii - and show it!
Then we do the same for the Bottom nibble... and show that as Ascii too!
We then repeat until B is Zero, to show all the bytes...
Much easier and faster than if we were storing HEX!
|Try the program to the right! Put a breakpoint on the first
command, and watch how DAA alters A and the Carry flag to keep things
DAA also uses the H Flag for its own purposes... but that's not something we need to worry about!
Basically, We can do ADD/ADC or SUB/SBC on a byte.. and DAA will fix things... we just need to alter the next byte if the carry flag is set!
so we've shown some numbers to the screen, but we actually need to be
able to do some maths too!... fortunately the Z80 allows us to use our
typical ADC and SBC commands to do addition and subtraction in Binary
Coded Decimal! How? well its simple actually!
|Now we know how to use DAA things aer pretty easy...
Lets create a function that adds BCD value at (HL) to the one at (DE)... for B bytes
We do OR A to clear the carry flag... then we process each byte (from smallest to biggest) using ADC to add the values...
DAA sorts out our numbers, and w estore the result, then we just repeat... ADC will add the carry if there was one caused by the last addition
|Subtraction is basically exactly the same, we just use SBC this time instead!|
|Finally, we may want to compare a BCD value to another, and see if they are equal, or which is higher...
If we start from the most significant byte, we can just do our CP to compare, returning as soon as we find a byte that is different.
If both are the same, we just clear the carry flag and return Z
|Ok We're done!
All the commands today are run in the same way, by passing the address of one or usually two parameters in HL and DE, and the length in B,
Remember, the BCD data is in reverse order!
If you want to see more, take a look at "Lesson_A1.asm" in the Samples.7z!
|This is not called
by the user directly, it's called by the other modules and shows HL as
hex, surrounded by ** symbols
If needed it will pause the system after showing the Breakpoint or registrer
|This module will pop a register and show it to
because we need HL, but don't want to use the stack we back it up with
self modifying code...
Then we pop a pair of the stack... this will be the return address... Next we swap HL with the pair on the stack... HL will now be the pair pushed before the call, and the return address will be at the top of the stack...
All that's left to do is show HL... then restore HL and return to our program!
|This module is super simple! all it does is get the return address off the stack.. show it, and return!|
|More complex than
the last version... this one overwrites the 3 bytes preceeding the
These will be the CALL command that called the monitor - the end result is that the monitor is only called once... so it can be used in a repeating loop to find out the program counter location (or if the loop even ran - if your program is crashing) without slowing down the program with lots of pauses.
| Lesson A2 - Interrupt Mode 2
We've looked at Interrupt mode 1 before in Lesson 7 ,but there's more stuff we need to cover before we've mastered them!
Lets take another look now, quickly at Interrupt Mode 0 (which is useless) Mode 1 again, and then we'll look at how to make use of the less used Interrupt Mode 2, and why it may really help you!
may see examples online that use a block of 257 &FF bytes in
the rom as the IM2 block, which would jump to &FFFF... this works
OK on the 48k, but does not work well on the 128k systems.
If you want to play it safe you can't do that... but using 257 bytes for the IM2 block may be tough on a 48k system!
|Why use &8000 and &8181?
The ZX spectrum has issues with IM2 if the range is lower than &8000, and (I think) higher than &C000... but the &8000/&8181 option is rock solid, and the choice of smarter minds than mine!... of course if you know what you're doing you can put the IM2 block somewhere else... but the &8000 range tends to be free on all systems, so this is as good a choice as there is!
Is there a way to stop IM2 using so many bytes?
No, there isn't... you'll have to use &8000-&8100 for the filled block, and &8181 for the start of the interrupt handler (you can just have a jump there to your real handler... one tip is you can use the range &8001-&8180 for your stack, to use up the 'spare space'... and reduce the pain of those lost bytes a bit!
|The Assembly code for using the IM2 interrupt handler is pretty easy, it just takes a lot of memory!
First we disable interrupts,
Then we put a jump to our interrupt code at &8181
Now we fill &8000-&8101 with the byte &81
We set I to &80, and the interrupt mode to IM2
for good measure, lets use the spare space for the stack pointer
we're done, so we just enable interrupts!
you don' t NEED to use interrupts if you don't want to! They help make
timing easy (for updating music) and allow you to switch colors
midscreen, but if you don't need things like that, just keep interrupts
You'll save some speed! and won't have to worry about all this sillyness