| 1 |
;; tmr0_16bit.asm |
| 2 |
;; |
| 3 |
;; The purpose of this program is to test how well gpsim can simulate |
| 4 |
;; TMR0 for a 16bit-core pic (like the 18fxxx family) |
| 5 |
;; Here are the tests performed: |
| 6 |
;; |
| 7 |
;; -- TMR0L and TMR0H can be read and written |
| 8 |
;; -- Writing to TMR0L |
| 9 |
|
| 10 |
list p=18f452 ; list directive to define processor |
| 11 |
include <p18f452.inc> ; processor specific variable definitions |
| 12 |
include <coff.inc> ; Grab some useful macros |
| 13 |
radix dec ; Numbers are assumed to be decimal |
| 14 |
|
| 15 |
include "delay.inc" ; Defines the "DELAY" macro |
| 16 |
|
| 17 |
|
| 18 |
;---------------------------------------------------------------------- |
| 19 |
|
| 20 |
; Printf Command |
| 21 |
.command macro x |
| 22 |
.direct "C", x |
| 23 |
endm |
| 24 |
|
| 25 |
;---------------------------------------------------------------------- |
| 26 |
GPR_DATA UDATA |
| 27 |
temp RES 1 |
| 28 |
temp1 RES 1 |
| 29 |
temp2 RES 1 |
| 30 |
failures RES 1 |
| 31 |
|
| 32 |
TMR0_RollOver RES 1 |
| 33 |
tmr0Lo RES 1 |
| 34 |
tmr0Hi RES 1 |
| 35 |
psa RES 1 |
| 36 |
b16bitMode RES 1 |
| 37 |
countLo RES 1 |
| 38 |
countHi RES 1 |
| 39 |
|
| 40 |
GLOBAL tmr0Lo, tmr0Hi |
| 41 |
GLOBAL psa,b16bitMode |
| 42 |
GLOBAL done |
| 43 |
GLOBAL TMR0_RollOver |
| 44 |
GLOBAL countLo, countHi |
| 45 |
|
| 46 |
|
| 47 |
;---------------------------------------------------------------------- |
| 48 |
; ******************* MAIN CODE START LOCATION ****************** |
| 49 |
;---------------------------------------------------------------------- |
| 50 |
STARTUP CODE 0 |
| 51 |
|
| 52 |
bra Start |
| 53 |
|
| 54 |
;------------------------------------------------------------------------ |
| 55 |
; |
| 56 |
; Interrupt Vector |
| 57 |
; |
| 58 |
;------------------------------------------------------------------------ |
| 59 |
|
| 60 |
INT_VECTOR CODE 0x008 ; interrupt vector location |
| 61 |
|
| 62 |
|
| 63 |
check_TMR0_interrupt: |
| 64 |
|
| 65 |
BTFSC INTCON,T0IF ;If the roll over flag is not set |
| 66 |
BTFSS INTCON,T0IE ;or the interrupt is not enabled |
| 67 |
RETFIE 1 ; Then leave |
| 68 |
|
| 69 |
;; TMR0 has rolled over. Clear the pending interrupt and notify |
| 70 |
;; the foreground code by incrementing the "roll over" counter |
| 71 |
;; Notice that the increment operation is atomic - this means |
| 72 |
;; we won't have to worry about the foreground code and interrupt |
| 73 |
;; code clashing over accesses to this variable. |
| 74 |
|
| 75 |
BCF INTCON,T0IF,0 ; Clear the pending interrupt |
| 76 |
INCF TMR0_RollOver,F ; Set a flag to indicate rollover |
| 77 |
|
| 78 |
ExitInterrupt: |
| 79 |
RETFIE 1 |
| 80 |
|
| 81 |
|
| 82 |
MAIN CODE |
| 83 |
|
| 84 |
Start: |
| 85 |
; At reset, all bits of t0Con should be set. |
| 86 |
.assert "t0con == 0xff" |
| 87 |
nop |
| 88 |
|
| 89 |
;------------------------------------------------------------ |
| 90 |
; The high byte of TMR0 is exposed to the firmware as a shadow register. When |
| 91 |
; firmware writes to this register, the value is latched. When the firmware |
| 92 |
; writes the to the low byte, then the latched high byte will get copied |
| 93 |
; along with the low byte to TMR0. This allows all 16-bits to be written in a |
| 94 |
; single cycle. Similarly, when TMR0L is read, the high byte of the timer |
| 95 |
; is copied to the shadowed register. |
| 96 |
|
| 97 |
TMR0H_ShadowRegisterTest: |
| 98 |
.command "echo *** Testing TMR0H" |
| 99 |
|
| 100 |
MOVLW 42 |
| 101 |
MOVWF TMR0H |
| 102 |
.assert "tmr0h == 42" |
| 103 |
nop |
| 104 |
|
| 105 |
MOVWF TMR0L |
| 106 |
.assert "tmr0l == 42" |
| 107 |
nop |
| 108 |
|
| 109 |
MOVF TMR0L,W |
| 110 |
.assert "tmr0l == 42" |
| 111 |
nop |
| 112 |
|
| 113 |
MOVF TMR0H,W |
| 114 |
.assert "tmr0h == 42" |
| 115 |
nop |
| 116 |
|
| 117 |
; Clear the shadowed TMR0H register and verify that it cleared. |
| 118 |
CLRF TMR0H |
| 119 |
.assert "tmr0h == 0" |
| 120 |
nop |
| 121 |
|
| 122 |
; Now, when we read the low byte of Timer 0, the high byte gets |
| 123 |
; refreshed. So let's check that TMR0H changes due to a TMR0L read: |
| 124 |
MOVF TMR0L,W |
| 125 |
.assert "tmr0l == 42" |
| 126 |
nop |
| 127 |
MOVF TMR0H,W |
| 128 |
.assert "tmr0h == 42" |
| 129 |
NOP |
| 130 |
|
| 131 |
TMR0_8BitModeTest: |
| 132 |
|
| 133 |
.command "echo *** Testing 8-bit mode" |
| 134 |
|
| 135 |
; Stop the timer and clear its value |
| 136 |
CLRF T0CON |
| 137 |
CLRF TMR0L |
| 138 |
CLRF TMR0H |
| 139 |
.assert "(tmr0l == 0) && (tmr0h == 0)" |
| 140 |
nop |
| 141 |
|
| 142 |
; The timer is off, so any value written to it should be read back |
| 143 |
; unchanged. |
| 144 |
MOVLW 42 |
| 145 |
MOVWF TMR0L |
| 146 |
.assert "tmr0l == 42" |
| 147 |
NOP |
| 148 |
|
| 149 |
MOVWF TMR0H |
| 150 |
.assert "tmr0h == 42" |
| 151 |
NOP |
| 152 |
MOVWF tmr0Hi |
| 153 |
;------------------------------------------------------------ |
| 154 |
; 8-bit mode tests |
| 155 |
; |
| 156 |
; First, TMR0 is tested in 8-bit mode and with interrupts disabled. |
| 157 |
; All 8 combinations of PSA are tested. |
| 158 |
|
| 159 |
CLRF psa ; Shadow's the PSA bits of T0CON |
| 160 |
|
| 161 |
L1_tmr0_8BitModeTest: ; Beginning of the loop |
| 162 |
|
| 163 |
; Start off with T0CON and TMR0L in a known state. |
| 164 |
|
| 165 |
.command "psa" |
| 166 |
CLRF T0CON |
| 167 |
CLRF TMR0L |
| 168 |
CLRF tmr0Lo |
| 169 |
|
| 170 |
; Assume that the PSA is not assigned to TMR0: |
| 171 |
|
| 172 |
MOVLW (1<<TMR0ON) | (1<<T08BIT) | (1<<PSA) |
| 173 |
|
| 174 |
; if psa is non-zero then the prescaler *is* assigned to tmr0 |
| 175 |
MOVF psa,F |
| 176 |
BZ L_psaInitComplete |
| 177 |
|
| 178 |
; psa is one greater than the actually Prescale bits |
| 179 |
DECF psa,W |
| 180 |
ANDLW 7 |
| 181 |
IORLW (1<<TMR0ON) | (1<<T08BIT) |
| 182 |
|
| 183 |
L_psaInitComplete: |
| 184 |
MOVWF T0CON |
| 185 |
; |
| 186 |
; tmr0 is running now. It should increment once every PSA cycles. |
| 187 |
; |
| 188 |
; First read the whole 16-bit counter note |
| 189 |
|
| 190 |
MOVF TMR0L,W |
| 191 |
MOVF TMR0H,W |
| 192 |
.assert "W == tmr0Hi" |
| 193 |
NOP |
| 194 |
|
| 195 |
; Now we'll check TMR0L after various delays. The assertion works |
| 196 |
; by reading tmr0l and comparing it to the number of cycles that |
| 197 |
; have gone by since the timer started. The shift right by 'psa' allows |
| 198 |
; the same assertion to be used as we loop through all combinations |
| 199 |
; of psa. |
| 200 |
|
| 201 |
; Single cycle checks. |
| 202 |
|
| 203 |
.assert "tmr0l == ((tmr0Lo+4)>>psa)" |
| 204 |
NOP |
| 205 |
.assert "tmr0l == ((tmr0Lo+5)>>psa)" |
| 206 |
NOP |
| 207 |
.assert "tmr0l == ((tmr0Lo+6)>>psa)" |
| 208 |
NOP |
| 209 |
.assert "tmr0l == ((tmr0Lo+7)>>psa)" |
| 210 |
NOP |
| 211 |
.assert "tmr0l == ((tmr0Lo+8)>>psa)" |
| 212 |
NOP |
| 213 |
.assert "tmr0l == ((tmr0Lo+9)>>psa)" |
| 214 |
NOP |
| 215 |
.assert "tmr0l == ((tmr0Lo+10)>>psa)" |
| 216 |
|
| 217 |
; 8 23-cycle delays. |
| 218 |
|
| 219 |
MOVLW 23 - 10 |
| 220 |
RCALL DelayCycles |
| 221 |
.assert "tmr0l == ((tmr0Lo+33)>>psa )" |
| 222 |
|
| 223 |
MOVLW 23 - 10 |
| 224 |
RCALL DelayCycles |
| 225 |
.assert "tmr0l == ((tmr0Lo+56)>>psa )" |
| 226 |
|
| 227 |
MOVLW 23 - 10 |
| 228 |
RCALL DelayCycles |
| 229 |
.assert "tmr0l == ((tmr0Lo+79)>>psa )" |
| 230 |
|
| 231 |
MOVLW 23 - 10 |
| 232 |
RCALL DelayCycles |
| 233 |
.assert "tmr0l == ((tmr0Lo+102)>>psa )" |
| 234 |
|
| 235 |
MOVLW 23 - 10 |
| 236 |
RCALL DelayCycles |
| 237 |
.assert "tmr0l == ((tmr0Lo+125)>>psa )" |
| 238 |
|
| 239 |
MOVLW 23 - 10 |
| 240 |
RCALL DelayCycles |
| 241 |
.assert "tmr0l == ((tmr0Lo+148)>>psa )" |
| 242 |
|
| 243 |
MOVLW 23 - 10 |
| 244 |
RCALL DelayCycles |
| 245 |
.assert "tmr0l == ((tmr0Lo+171)>>psa )" |
| 246 |
|
| 247 |
MOVLW 23 - 10 |
| 248 |
RCALL DelayCycles |
| 249 |
.assert "tmr0l == ((tmr0Lo+194)>>psa )" |
| 250 |
|
| 251 |
; 255 and 256 cycle boundary condition |
| 252 |
|
| 253 |
MOVLW 61 - 10 |
| 254 |
RCALL DelayCycles |
| 255 |
.assert "tmr0l == ((tmr0Lo+255)>>psa )" |
| 256 |
NOP |
| 257 |
|
| 258 |
.assert "tmr0l == (((tmr0Lo+256)>>psa)&0xff)" |
| 259 |
nop |
| 260 |
|
| 261 |
; now for some bigger delays. |
| 262 |
|
| 263 |
DELAY 250 |
| 264 |
.assert "tmr0l == (((tmr0Lo+256+251)>>psa)&0xff)" |
| 265 |
nop |
| 266 |
|
| 267 |
DELAY 250 |
| 268 |
.assert "tmr0l == (((tmr0Lo+256+2*251)>>psa)&0xff)" |
| 269 |
nop |
| 270 |
|
| 271 |
DELAY 250 |
| 272 |
.assert "tmr0l == (((tmr0Lo+256+3*251)>>psa)&0xff)" |
| 273 |
nop |
| 274 |
|
| 275 |
DELAY 250 |
| 276 |
.assert "tmr0l == (((tmr0Lo+256+4*251)>>psa)&0xff)" |
| 277 |
nop |
| 278 |
|
| 279 |
DELAY 250 |
| 280 |
.assert "tmr0l == (((tmr0Lo+256+5*251)>>psa)&0xff)" |
| 281 |
nop |
| 282 |
|
| 283 |
DELAY 250 |
| 284 |
.assert "tmr0l == (((tmr0Lo+256+6*251)>>psa)&0xff)" |
| 285 |
nop |
| 286 |
|
| 287 |
DELAY 250 |
| 288 |
.assert "tmr0l == (((tmr0Lo+256+7*251)>>psa)&0xff)" |
| 289 |
nop |
| 290 |
|
| 291 |
DELAY 250 |
| 292 |
.assert "tmr0l == (((tmr0Lo+256+8*251)>>psa)&0xff)" |
| 293 |
nop |
| 294 |
|
| 295 |
DELAY 8190 |
| 296 |
.assert "tmr0l == (((tmr0Lo+256+8*251+8191)>>psa)&0xff)" |
| 297 |
nop |
| 298 |
|
| 299 |
INCF psa,F |
| 300 |
btfss psa,3 |
| 301 |
bra L1_tmr0_8BitModeTest |
| 302 |
|
| 303 |
; Through looping through all combinations of PSA. |
| 304 |
; Now make sure that the high byte of TMR0 hasn't changed. |
| 305 |
MOVF TMR0H,W |
| 306 |
.assert "W == tmr0Hi" |
| 307 |
NOP |
| 308 |
|
| 309 |
|
| 310 |
;------------------------------------------------------------ |
| 311 |
; Interrupt tests |
| 312 |
|
| 313 |
NOP |
| 314 |
NOP |
| 315 |
NOP |
| 316 |
|
| 317 |
|
| 318 |
CLRF T0CON ;Stop TMR0 |
| 319 |
CLRF INTCON ;Clear any pending interrupts |
| 320 |
BSF INTCON,T0IE ;Enable TMR0 overflow interrupts |
| 321 |
BSF INTCON,GIE ;Enable global interrupts |
| 322 |
|
| 323 |
CLRF psa ;Loop counter and prescaler assignment. |
| 324 |
|
| 325 |
L1_InterruptTest: |
| 326 |
|
| 327 |
; Stop and clear TMR0: |
| 328 |
CLRF T0CON |
| 329 |
CLRF TMR0H |
| 330 |
CLRF TMR0L |
| 331 |
CLRF TMR0_RollOver ;Interrupt flag |
| 332 |
|
| 333 |
;Software counter to count cycles |
| 334 |
|
| 335 |
SETF countLo ;Start off with counter==-1 |
| 336 |
SETF countHi |
| 337 |
|
| 338 |
; Assume that the PSA is not assigned to TMR0: |
| 339 |
|
| 340 |
MOVLW (1<<TMR0ON) | (1<<T08BIT) | (1<<PSA) |
| 341 |
|
| 342 |
; if psa is non-zero then the prescaler *is* assigned to tmr0 |
| 343 |
MOVF psa,F |
| 344 |
BZ L_psaInitComplete2 |
| 345 |
|
| 346 |
; psa is one greater than the actually Prescale bits |
| 347 |
DECF psa,W |
| 348 |
ANDLW 7 |
| 349 |
IORLW (1<<TMR0ON) | (1<<T08BIT) |
| 350 |
L_psaInitComplete2: |
| 351 |
MOVWF T0CON |
| 352 |
|
| 353 |
; Now enter into a loop and wait for the interrupt. |
| 354 |
; Each loop iteration takes exactly 8 cycles. |
| 355 |
|
| 356 |
TMR0_WaitForInt: |
| 357 |
NOP |
| 358 |
NOP |
| 359 |
clrwdt |
| 360 |
INFSNZ countLo,F |
| 361 |
INCF countHi,F |
| 362 |
BTFSS TMR0_RollOver,0 |
| 363 |
bra TMR0_WaitForInt |
| 364 |
|
| 365 |
.assert " (((countHi<<8)+countLo)>>(5+psa)) == 1" |
| 366 |
nop |
| 367 |
INCF psa,F |
| 368 |
btfss psa,3 |
| 369 |
bra L1_InterruptTest |
| 370 |
|
| 371 |
; |
| 372 |
; test that t0 continues to run after sleep woken up by t1 |
| 373 |
nop |
| 374 |
bsf T0CON,PSA ; bypass precounter |
| 375 |
bsf T1CON,T1SYNC ;sync off |
| 376 |
bsf T1CON,TMR1ON ; start timer 1 |
| 377 |
bsf PIE1,TMR1IE ; turn on interrupt |
| 378 |
sleep |
| 379 |
movf TMR0L,W |
| 380 |
bcf T1CON,TMR1ON ; stop timer 1 |
| 381 |
nop |
| 382 |
nop |
| 383 |
.assert "tmr0l != W, '*** FAILED 16bit-core TMR0 test- TMR0 stopped after sleep'" |
| 384 |
nop |
| 385 |
|
| 386 |
; |
| 387 |
; TMR0 should not interrupt during sleep as it is turned off |
| 388 |
; The WDT cancels the sleep |
| 389 |
; |
| 390 |
clrf TMR0_RollOver |
| 391 |
sleep |
| 392 |
nop |
| 393 |
.assert "TMR0_RollOver == 0, '*** FAILED 16bit-core TMR0 test- TMR0 interrupt in sleep'" |
| 394 |
nop |
| 395 |
|
| 396 |
;------------------------------------------------------------ |
| 397 |
; 16bit-mode tests |
| 398 |
|
| 399 |
CLRF psa |
| 400 |
|
| 401 |
L1_tmr0_16BitModeTest: |
| 402 |
|
| 403 |
CLRF T0CON |
| 404 |
CLRF TMR0H |
| 405 |
CLRF TMR0L |
| 406 |
|
| 407 |
; Assume that the PSA is not assigned to TMR0: |
| 408 |
|
| 409 |
MOVLW (1<<TMR0ON) | (1<<PSA) |
| 410 |
|
| 411 |
; if psa is non-zero then the prescaler *is* assigned to tmr0 |
| 412 |
MOVF psa,F |
| 413 |
BZ L_psa16bitInitComplete |
| 414 |
|
| 415 |
; psa is one greater than the actually Prescale bits |
| 416 |
DECF psa,W |
| 417 |
ANDLW 7 |
| 418 |
IORLW (1<<TMR0ON) |
| 419 |
|
| 420 |
L_psa16bitInitComplete: |
| 421 |
.command "psa" |
| 422 |
MOVWF T0CON |
| 423 |
|
| 424 |
|
| 425 |
MOVF TMR0L,W |
| 426 |
.assert "( ((tmr0h<<8)+W) == (1>>psa))" |
| 427 |
|
| 428 |
MOVF TMR0L,W |
| 429 |
.assert "( ((tmr0h<<8)+W) == (2>>psa))" |
| 430 |
|
| 431 |
MOVF TMR0L,W |
| 432 |
.assert "( ((tmr0h<<8)+W) == (3>>psa))" |
| 433 |
|
| 434 |
MOVF TMR0L,W |
| 435 |
.assert "( ((tmr0h<<8)+W) == (4>>psa))" |
| 436 |
|
| 437 |
MOVF TMR0L,W |
| 438 |
.assert "( ((tmr0h<<8)+W) == (5>>psa))" |
| 439 |
|
| 440 |
|
| 441 |
MOVF TMR0L,W |
| 442 |
MOVWF tmr0Lo |
| 443 |
|
| 444 |
.command "tmr0Lo" |
| 445 |
.command "tmr0h" |
| 446 |
|
| 447 |
DELAY 1024-2 |
| 448 |
MOVF TMR0L,W |
| 449 |
MOVWF tmr0Lo |
| 450 |
|
| 451 |
.command "tmr0Lo" |
| 452 |
.command "tmr0h" |
| 453 |
|
| 454 |
DELAY 8192-2 |
| 455 |
MOVF TMR0L,W |
| 456 |
MOVWF tmr0Lo |
| 457 |
|
| 458 |
.command "tmr0Lo" |
| 459 |
.command "tmr0h" |
| 460 |
|
| 461 |
INCF psa,F |
| 462 |
btfss psa,3 |
| 463 |
bra L1_tmr0_16BitModeTest |
| 464 |
|
| 465 |
nop |
| 466 |
done: |
| 467 |
.assert "'*** PASSED 16bit-core TMR0 test'" |
| 468 |
bra $ |
| 469 |
|
| 470 |
failed: |
| 471 |
movlw 1 |
| 472 |
movwf failures |
| 473 |
.assert "'*** FAILED 16bit-core TMR0 test'" |
| 474 |
bra done |
| 475 |
|
| 476 |
end |