drogon wrote:
daniMolina wrote:
I'm going to start working on it this weekend, so I'm not looking for a solution, but I wanted to hear other people's approaches to this problem.
Keeping it simple:
Code:
Disable interrupts (if you're using them at this point)
Set a 32-bit counter to zero.
Poll the RTC until you see a change in the 0.1s register.
Loop
Increment 32-bit counter
Until the RTC 0.1s register changes again.
Enable interrupts (if using them at this point)
This has been (with some changes, and a weird math accident) my approach in the end
Code:
ldx #$56 // Counter LO prescaler
stx $A0 // Counter LO stored
ldy #$08 // Counter HI prescaler
sty $A1 // Counter HI stored
lda #$00
sta $A2 // Result location
lda CIA_TODT
w1: cmp CIA_TODT
beq w1 // Wait for 10ths to change
lda CIA_TODT // Store new 10ths value
n1: dec $A0 // Dec LO counter
bne n2 // If not 0, end
stx $A0 // If 0, restore LO
dec $A1 // and dec HI counter
bne n2 // If not 0, end
sty $A1 // If 0, restore HI
inc $A2 // and increase result
n2: cmp CIA_TODT // Has 10ths changed?
beq n1 // If not, continue
I initialize a 16 bit counter (A0,A1 in zp) to some value.
Then check current 10ths of second in TOD register, and wait for it to change.
Repeat
Decrease my counter by 1. Once it reaches 0, increase the result (stored in A2)
until 10ths changes again
A0 and A1 it's not a 16 bit counter, as every time A0 reaches 0, I reload the initial value, so they're actually to 8 bit counters chained together.
At the end of the loop, the result in A2 is in hundreds of KHz
10 for 1.0 Mhz
11 for 1.1 Mhz
...
I've tested it between 500 KHz and 1.2 Mhz with an original 6526 and seems accurate.
I tried to get the value for the counter with a somewhat "scientific" approach, calculating the cycles the loop needed and so on... and I was off by almost 10x... so it ended being a trial and error. More a quick hack than a verified solution, but good enough for my needs.
My TTL 6526 still has no TOD implemented, so I'll have to wait (for myself to build it!) to test this at higher speeds.
Thanks for all your tips!