Mac BeebEm4 - Decimal Mode and the System Clock

want to talk about MESS/model b/beebem/b-em/electrem/elkulator? do it here!
User avatar
EdwardianDuck
Posts: 47
Joined: Thu Aug 10, 2017 8:07 pm
Location: Northamptonshire
Contact:

Mac BeebEm4 - Decimal Mode and the System Clock

Postby EdwardianDuck » Sun Dec 24, 2017 5:00 pm

I've finally got round to doing a little work on a programming project and wanted to get some timings as I experiment with various optimisations. So, being somewhat of a BBC neophyte, I took the obvious route of reading the system clock using OSWORD 1 at the start and end, subtracting one from the other and displaying the difference. So far so good. However, I noticed that the times were not only somewhat variable for the same test case, but more often than not simply wrong.

Printing out the current value of the time in hex as part of the main loop and watching it (clearly I have far too much time on my hands) I noticed that 2nd byte would "jump" rather than increment when the first byte rolled over. After a couple of goes it became clear that it was jumping from $(x)A to $(x+1)0 instead of $(x)B, which suggests the 65c02 is in decimal mode during whatever interrupt updates the clock.

Well that's possible, I'm using code from 6502.org which converts a number to a string by using BCD mode, so it seems that the system interrupt isn't clearing the decimal flag when it interrupts my conversion routine. I don't know whether I should expect it to or not.

If I remove the code which depends on BCD mode it works. It also works if I disable/enable interrupts in my conversion code (although that doesn't seem like the right thing to do).

On real hardware (Master 128), I'm not seeing this behaviour, which makes me think it's a BeebEm problem.

I get the same behaviour on BeebEm 3.3.

Any thoughts, or am I just missing some subtle point?

Jeremy
Master 128 + RetroClinic DataCentre + Internal Pi Zero Coprocessor, MiST

User avatar
BigEd
Posts: 1728
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Mac BeebEm4 - Decimal Mode and the System Clock

Postby BigEd » Sun Dec 24, 2017 5:49 pm

The 65c02 - unlike the 6502 - clears decimal mode when an interrupt is taken. So code for an interrupt handler which is certainly running on a 65c02 need not CLD, whereas code for a general 6502 must do a CLD if it is going to use ADC or SBC.

Of course, when the RTI comes there's an implicit PLP, so the state of the decimal flag is restored for the main code.

Sounds like the emulator in this case isn't clearing the D flag for an interrupt.

User avatar
EdwardianDuck
Posts: 47
Joined: Thu Aug 10, 2017 8:07 pm
Location: Northamptonshire
Contact:

Re: Mac BeebEm4 - Decimal Mode and the System Clock

Postby EdwardianDuck » Sun Dec 24, 2017 6:14 pm

Thank you for the information, that's really helpful. I hadn't picked up on the interrupt/decimal mode difference.

Jeremy
Master 128 + RetroClinic DataCentre + Internal Pi Zero Coprocessor, MiST