NewIRQs 28 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
; Copyright 1996 Acorn Computers Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
        TTL     => NewIRQs

; *****************************************************************************
;
; Main IRQ routine:
;     Push  workregs,lr
;     IRQsema -> TOS
;     stack -> IRQsema
;     call IRQ1V
;     IRQs off
;     TOS ->IRQsema
;     process callback, pulling workregs,pc at some point
;
; *****************************************************************************

Neil Turton's avatar
Neil Turton committed
30
        ALIGN 32
31 32 33 34

Initial_IRQ_Code ROUT
        SUB     lr, lr, #4
        Push    "r0, lr"
35
        MRS     lr, SPSR
36 37 38 39
        Push    "r1-r3, r11, r12, lr"
; ** For Pete's sake remember to change the heap manager if you change the above
; ** register list!!!!!!! And the [sp_irq, #4*5] below

Kevin Bracey's avatar
Kevin Bracey committed
40
  [ :LNOT:No26bitCode
41
        BIC     r0, lr, #&1F            ; clear out foreground mode bits
Kevin Bracey's avatar
Kevin Bracey committed
42
        ORR     r0, r0, #I32_bit + IRQ26_mode ; force IRQ_26 mode and I bit set
43
        MSR     CPSR_c, r0
Kevin Bracey's avatar
Kevin Bracey committed
44
  ]
45

Jeffrey Lee's avatar
Jeffrey Lee committed
46
        LDR     r12, =ZeroPage
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        LDR     r0, [r12, #IRQsema]
        Push    r0
        STR     sp_irq, [r12, #IRQsema]
        MOV     lr, pc
        LDR     pc, [r12, #IRQ1V]

; IRQ1V called with r0-r3,r11,r12 trashable. r12=0

; Stu has a theory that 1N cycle can be saved by the default IRQ1V pointing
; at a location containing a branch to our code; we then do something like
;  LDR R0, [R12, #IRQ1V]
;  CMP R0, #OurIRQ1V
;  BNE somebodysonIRQ1V
;  .... fall into default IRQ1V code

62 63 64 65
        Push     "r10"
        MOV      r10, #UnthreadV
        BL       CallVector
        Pull     "r10"
66

Jeffrey Lee's avatar
Jeffrey Lee committed
67
        LDR      r11, =ZeroPage
68 69
        Pull     r0
        STR      r0, [r11, #IRQsema]
70

Kevin Bracey's avatar
Kevin Bracey committed
71
  [ :LNOT:No26bitCode
72
        MRS      r0, CPSR
73
        ORR      r0, r0, #&10
74
        MSR      CPSR_c, r0                     ; switch back to IRQ32 mode
Kevin Bracey's avatar
Kevin Bracey committed
75
  ]
76 77 78 79

        LDRB     r11, [r11, #CallBack_Flag]
        TEQ      r11, #0
        Pull     "r1-r3, r11, r12, lr", EQ
80
        MSREQ    SPSR_cxsf, lr
81 82 83 84 85 86
        Pull     "r0, pc", EQ, ^

        TST      r11, #CBack_Postpone
        LDREQ    lr, [sp_irq, #4*5]     ; get SPSR off stack
        TSTEQ    lr, #I32_bit :OR: &0F  ; check we came from USR26 or USR32 mode, with IRQs enabled
        Pull     "r1-r3, r11, r12, lr", NE
87
        MSRNE    SPSR_cxsf, lr
88 89
        Pull     "r0, pc", NE, ^

Kevin Bracey's avatar
Kevin Bracey committed
90
; Do a CallBack: asked for, not postponed, and we're returning into USR26/32 mode.
91 92 93 94

        ASSERT   IRQ32_mode :AND: SVC32_mode = IRQ32_mode ; so the following dodgy ops work

        Pull     "r1-r3, r11, r12"
95
        MRS      r0, CPSR
96
        ORR      r0, r0, #SVC32_mode
97
        MSR      CPSR_c, r0
98 99
        Push     "r10-r12"                               ; push r10-r12 onto the SVC stack
        BIC      r0, r0, #IRQ32_mode :EOR: SVC32_mode
100
        MSR      CPSR_c, r0
101
        Pull     "r10-r12"                      ; SPSR, R0, LR really
Ben Avison's avatar
Ben Avison committed
102
 [ No26bitCode :LOR: FixCallBacks
103
        ORR      r0, r0, #SVC32_mode
Kevin Bracey's avatar
Kevin Bracey committed
104
 |
105 106
        BIC      r0, r0, #&1F
        ORR      r0, r0, #SVC26_mode
Neil Turton's avatar
Neil Turton committed
107
 ]
108
        MSR      CPSR_c, r0
Kevin Bracey's avatar
Kevin Bracey committed
109 110 111
        Push     r12                            ; Save the return address
        MOV      r14, r10                       ; SPSR into R14
        MOV      r0, r11                        ; restore original R0
Jeffrey Lee's avatar
Jeffrey Lee committed
112
        LDR      r10, =ZeroPage
Neil Turton's avatar
Neil Turton committed
113
        LDRB     r11, [r10, #CallBack_Flag]
Ben Avison's avatar
Ben Avison committed
114 115 116
      [ FixCallBacks
        B        Do_CallBack_postpone_already_clear
      |
Neil Turton's avatar
Neil Turton committed
117
        B        Do_CallBack
Ben Avison's avatar
Ben Avison committed
118
      ]
Neil Turton's avatar
Neil Turton committed
119 120 121 122 123 124 125 126

        LTORG

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Default IRQ1V: despatch on interrupting device

; Now copied to RAM, together with vector entries and device tables

127 128
        ASSERT    HAL ; Pre-HAL code is gone. Sorry!

Kevin Bracey's avatar
Kevin Bracey committed
129 130 131 132 133 134 135
                  ^ 0
IRQDesp_Link      # 4
IRQDesp_R12Val    # 4
IRQDesp_CallAddr  # 4

IRQDesp_Link_Unshared * 1       ; flag in Link (for _this_ node)

136
       ASSERT   IRQDesp_CallAddr = IRQDesp_R12Val + 4
Neil Turton's avatar
Neil Turton committed
137

Neil Turton's avatar
Neil Turton committed
138 139
DefaultIRQ1Vcode ROUT

Kevin Bracey's avatar
Kevin Bracey committed
140
        Push    "r9,lr"
Jeffrey Lee's avatar
Jeffrey Lee committed
141 142 143 144 145 146
      [ ZeroPage = 0
        MOV     r9, #0
      |
        LDR     r9, %FT02
      ]
        AddressHAL r9                   ; modifies r9
147
        CallHAL HAL_IRQSource
148
        Pull    "r9"
Kevin Bracey's avatar
Kevin Bracey committed
149
        ADR     r2, Devices
150
        ADD     r1, r0, r0, LSL #1      ; multiply by 3
Kevin Bracey's avatar
Kevin Bracey committed
151 152 153 154 155 156
        ADD     r11, r2, r1, LSL #2     ; so table contains DevNo * 3
01      MOV     lr, pc
        LDMIA   r11, {r11, r12, pc}
        TST     r11, #IRQDesp_Link_Unshared
        BEQ     %BT01
        Pull    pc
Jeffrey Lee's avatar
Jeffrey Lee committed
157 158 159 160
      [ ZeroPage <> 0
02
        DCD     ZeroPage
      ]
Neil Turton's avatar
Neil Turton committed
161 162

; ******* IRQ device handlers entered with r0-r3,r11,r12,r14 trashable *******
163
;   r0  =  device number (if HAL)
164
;   r3  -> IOC (not in HAL world, unless IOMD HAL is helpful)
Neil Turton's avatar
Neil Turton committed
165 166 167
;   r12 =  what they asked for
;   r14 =  return address to MOS IRQ exit sequence

168 169
DefaultIRQ1Vcode_end

Kevin Bracey's avatar
Kevin Bracey committed
170
Devices * DefaultIRQ1Vcode_end + 12
171

Kevin Bracey's avatar
Kevin Bracey committed
172
NoInterrupt     * -1
173

174
DevicesEnd * Devices + MaxInterrupts * 12
175

176
        ASSERT  DevicesEnd - DefaultIRQ1Vcode <= DefIRQ1Vspace
177 178 179 180 181

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

InitialiseIRQ1Vtable

182
        Push    "v1-v3,sb,lr"
183 184 185 186 187 188 189 190 191 192 193 194 195
; copy IRQ handler: not done with rest of copying
; because soft break needs the info to free any claimed blocks.

        LDR     a1, =DefaultIRQ1V
        ADRL    a2, DefaultIRQ1Vcode
        ADRL    a3, DefaultIRQ1Vcode_end
CopyDefaultIRQ1V
        LDR     a4, [a2], #4
        STR     a4, [a1], #4
        CMP     a2, a3
        BNE     CopyDefaultIRQ1V

        AddressHAL
Kevin Bracey's avatar
Kevin Bracey committed
196 197
        ADD     v1, a1, #12
        ADD     a3, v1, #MaxInterrupts*12
198
        MOV     a2, #0
Kevin Bracey's avatar
Kevin Bracey committed
199 200
        MOV     a4, #-1
        LDR     ip, =IRQ
201 202
FillInDefaultIRQ1VDevices
        STMIA   a1!, {a2, a4, ip}
Kevin Bracey's avatar
Kevin Bracey committed
203
        ADD     a4, a4, #1
204 205 206 207 208 209
        CMP     a1, a3
        BNE     FillInDefaultIRQ1VDevices

; Now fill in our basic device handlers. First the timer.
        MOV     a1, #0
        CallHAL HAL_TimerDevice
Jeffrey Lee's avatar
Jeffrey Lee committed
210
        LDR     a2, =ZeroPage+OsbyteVars
211 212 213
        LDR     a3, =TickOne
        ADD     a1, a1, a1, LSL #1
        ADD     a1, v1, a1, LSL #2
Kevin Bracey's avatar
Kevin Bracey committed
214
        STMIB   a1, {a2, a3}
215 216 217 218

; Now the VSync
        MOV     a1, #0
        CallHAL HAL_VideoFlybackDevice
Kevin Bracey's avatar
Kevin Bracey committed
219
        CMP     a1, #-1
Jeffrey Lee's avatar
Jeffrey Lee committed
220
        LDRNE   a2, =ZeroPage+OsbyteVars
Kevin Bracey's avatar
Kevin Bracey committed
221 222 223 224
        LDRNE   a3, =VsyncIRQ
        ADDNE   a1, a1, a1, LSL #1
        ADDNE   a1, v1, a1, LSL #2
        STMNEIB a1, {a2, a3}
John Ballance's avatar
John Ballance committed
225 226

1
227
 [ CDVPoduleIRQs
Kevin Bracey's avatar
Kevin Bracey committed
228 229
; Now Podule bits
        MOV     a1, #IRQDesp_Link_Unshared
Jeffrey Lee's avatar
Jeffrey Lee committed
230
        LDR     a2, =ZeroPage+PFIQasIRQ_Chain - (PodDesp_Link-PodDesp_R12Val)
Kevin Bracey's avatar
Kevin Bracey committed
231 232 233
        ADR     a3, PFIQasIRQ_Despatch
        ADD     lr, v1, #8*12
        STMIA   lr, {a1, a2, a3}
Jeffrey Lee's avatar
Jeffrey Lee committed
234
        LDR     a2, =ZeroPage+PIRQ_Chain - (PodDesp_Link-PodDesp_R12Val)
Kevin Bracey's avatar
Kevin Bracey committed
235 236 237 238 239 240
        ADR     a3, PIRQ_Despatch
        ADD     lr, v1, #13*12
        STMIA   lr, {a1, a2, a3}
 ]

; Now IIC - if any
Jeffrey Lee's avatar
Jeffrey Lee committed
241
        LDR     v2, =ZeroPage+IICBus_Base
242 243 244
        MOV     v3, #0
80
        LDR     a1, [v2, #IICBus_Type]
Kevin Bracey's avatar
Kevin Bracey committed
245 246 247 248 249
        TST     a1, #IICFlag_HighLevel
        TSTNE   a1, #IICFlag_Background
        BEQ     %FT90
        SUB     sp, sp, #12
        MOV     a1, sp
250
        MOV     a2, v3
Kevin Bracey's avatar
Kevin Bracey committed
251 252 253 254
        CallHAL HAL_IICDevice
; I think it's safe to call A SWI here...
        LDMIA   sp!, {r0, r3, r4}
        LDR     r1, =IICIRQ
255
        MOV     r2, v3
Kevin Bracey's avatar
Kevin Bracey committed
256 257
        SWI     XOS_ClaimDeviceVector
90
258 259 260 261
        ADD     v2, v2, #IICBus_Size
        ADD     v3, v3, #1
        CMP     v3, #IICBus_Count
        BNE     %BT80
262

263
        Pull    "v1-v3,sb,pc"
264 265


Neil Turton's avatar
Neil Turton committed
266 267 268 269 270 271 272
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Specialist despatchers for podules

                  ^  0
PodDesp_Address   #  4     ; address of IRQ status byte
PodDesp_Mask      #  4     ; for use on above
PodDesp_R12Val    #  4
Kevin Bracey's avatar
Kevin Bracey committed
273
PodDesp_CallAddr  #  4     ; address to call if (?Address EOR (Mask>>8)) AND Mask <> 0
Neil Turton's avatar
Neil Turton committed
274 275 276
PodDesp_Link      #  4     ; next node
PodDesp_NodeSize  #  0

Kevin Bracey's avatar
Kevin Bracey committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
 [ HAL:LAND:{FALSE}
        ROUT
; In    r1 -> top level node
;       r12 = sub chain
01      LDR     r12, [r12, #PodDesp_Link-PodDesp_R12Val]
SubInterrupt_Despatch
        LDMIA   r12!, {r2, r3}          ; address and mask
        CMP     r3, #&10000
        LDRLOB  r2, [r2]
        BHS     %FT02
        EOR     r2, r2, r3, LSR #8      ; polarity inversion
        TST     r2, r3                  ; check against mask
        BEQ     %BT01
        LDMIA   r12, {r12, pc}
02
        Push    "r0,r1,r12,lr"
        MOV     r0, r3
        MOV     r12, r3
295
      [ NoARMv5
Kevin Bracey's avatar
Kevin Bracey committed
296 297
        MOV     lr, pc
        MOV     pc, r2
298 299 300
      |
        BLX     r2
      ]
Kevin Bracey's avatar
Kevin Bracey committed
301 302 303 304 305 306 307 308 309 310
        TEQ     r0, #0
        Pull    "r0,r1,r12,lr"
        BEQ     %BT01
        LDMIA   r12, {r12, pc}


NotSubInterrupt
        LDR     r1, [r1, #8]            ; call next (full) handler
        LDMIA   r1, {r12, pc}
 |
Neil Turton's avatar
Neil Turton committed
311 312 313 314 315 316 317 318 319 320 321 322

; In    r12 =    PFIQasIRQ_Chain - (PodDesp_Link-PodDesp_R12Val)
;             or PIRQ_Chain - (PodDesp_Link-PodDesp_R12Val)     from despatcher

PFIQasIRQ_Despatch ROUT
PIRQ_Despatch ; All the same thing now

01      LDR     r12, [r12, #PodDesp_Link-PodDesp_R12Val]
        LDMIA   r12!, {r1, r2}           ; address and mask

; TMD 09-Jun-89: Don't corrupt r0 - it's needed by the default IRQ2 routine
        LDRB    r1, [r1]
Robert Sprowson's avatar
Robert Sprowson committed
323
        EOR     r1, r1, r2, LSR #8
Neil Turton's avatar
Neil Turton committed
324 325 326
        ANDS    r1, r1, r2
        BEQ     %BT01
        LDMIA   r12, {r12, pc}
Kevin Bracey's avatar
Kevin Bracey committed
327
 ]
Neil Turton's avatar
Neil Turton committed
328

Kevin Bracey's avatar
Kevin Bracey committed
329
Default_SubInterruptHandler_Node
Neil Turton's avatar
Neil Turton committed
330 331 332 333 334
Default_PIRQHandler_Node
Default_PFIQasIRQHandler_Node
        &       .+4                     ; address we know has non-zero value!
        &       -1                      ; mask
        &       0                       ; handler r12
Kevin Bracey's avatar
Kevin Bracey committed
335 336 337
 [ HAL:LAND:{FALSE}
        &       NotSubInterrupt         ; handler code
 |
Neil Turton's avatar
Neil Turton committed
338
        &       IRQ                     ; handler code
Kevin Bracey's avatar
Kevin Bracey committed
339
 ]
Neil Turton's avatar
Neil Turton committed
340 341 342 343 344 345 346 347
        &       0                       ; null link for naff release checking

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Claim of device vectors

; r0 = Device number
; r1 = call address
; r2 = r12 value
Kevin Bracey's avatar
Kevin Bracey committed
348 349 350 351
 [ HAL
; r3 = interrupt location     or r3 = 0 or (if r4 > 64K)  r3 = routine
; r4 = interrupt mask/polarity                            r4 = workspace
 |
352
  [ CDVPoduleIRQs
Neil Turton's avatar
Neil Turton committed
353 354
; r0 = PFIQ|PIRQ devno -> r3 = interrupt location
;                         r4 = interrupt mask
355
  ]
Kevin Bracey's avatar
Kevin Bracey committed
356 357 358 359
 ]

CDV_Flags * &FF000000
CDV_Shared * 1:SHL:31
Neil Turton's avatar
Neil Turton committed
360 361 362 363 364 365 366 367 368

DeviceVector_Claim ROUT

        Push    "r0-r3, lr"

01      SWI     XOS_ReleaseDeviceVector ; Release until bored
        BVC     %BT01

        LDR     r0, [sp]
Kevin Bracey's avatar
Kevin Bracey committed
369 370 371
 [ HAL
        BIC     r0, r0, #CDV_Flags
 ]
372 373 374
        LDR     lr, =ZeroPage
        LDR     lr, [lr, #IRQMax]
        CMP     r0, lr
Kevin Bracey's avatar
Kevin Bracey committed
375
        BHS     DV_Fail_NaffDevNo
Neil Turton's avatar
Neil Turton committed
376

Kevin Bracey's avatar
Kevin Bracey committed
377 378 379 380
 [ HAL:LAND:{FALSE}
;        TEQ     r3, #0
;        BNE     SubInterruptClaim
 |
381
  [ CDVPoduleIRQs
Neil Turton's avatar
Neil Turton committed
382 383 384
        CMP     r0, #Podule_DevNo
        CMPNE   r0, #PFIQasIRQ_DevNo
        BEQ     PoduleChainClaim
385
  ]
Kevin Bracey's avatar
Kevin Bracey committed
386
 ]
Neil Turton's avatar
Neil Turton committed
387 388 389 390

        MOV     r3, #12
        BL      ClaimSysHeapNode
        BVS     DV_Exit
Kevin Bracey's avatar
Kevin Bracey committed
391 392
        LDR     r11, [sp]
        BIC     r0, r11, #CDV_Flags
Neil Turton's avatar
Neil Turton committed
393 394 395
        ADD     r0, r0, r0, LSL #1      ; *3
        LDR     r1, =DefaultIRQ1V-DefaultIRQ1Vcode+Devices
        ADD     r1, r1, r0, LSL #2
Kevin Bracey's avatar
Kevin Bracey committed
396
        WritePSRc SVC_mode+I_bit, r10   ; IRQs off for update (on again on SWI exit)
Neil Turton's avatar
Neil Turton committed
397 398
        LDMIA   r1, {r0, r3, r10}
        STMIA   r2, {r0, r3, r10}       ; copy current head into node
Kevin Bracey's avatar
Kevin Bracey committed
399 400 401 402 403 404 405
 [ HAL
        MOV     r10, r2
        TST     r11, #CDV_Shared
        ORREQ   r10, r10, #IRQDesp_Link_Unshared
        LDR     r11, [sp, #4*2]         ; r12 value
        LDR     r12, [sp, #4*1]         ; call address
 |
Neil Turton's avatar
Neil Turton committed
406 407 408
        LDR     r10, [sp, #4*2]         ; r12 value
        LDR     r11, [sp, #4*1]         ; call address
        MOV     r12, r2
Kevin Bracey's avatar
Kevin Bracey committed
409
 ]
Neil Turton's avatar
Neil Turton committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
        STMIA   r1, {r10-r12}           ; copy given info into head


DV_Exit STRVS   r0, [sp]                ; Common exit for both claim + release
        Pull    "r0-r3, lr"
        B       SLVK_TestV


DV_Fail_NaffDevNo
        ADR     r0, ErrorBlock_NaffDevNo
      [ International
        BL      TranslateError
      |
        SETV
      ]
        B       DV_Exit

        MakeErrorBlock NaffDevNo

Kevin Bracey's avatar
Kevin Bracey committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
 [ HAL:LAND:{FALSE}
SubInterruptClaim
        Push    "r5"
        ADD     r0, r0, r0, LSL #1      ; *3
        LDR     r5, =DefaultIRQ1V-DefaultIRQ1Vcode+Devices
        ADD     r5, r5, r0, LSL #2
        LDR     r2, [r5, #IRQDesp_CallAddr]
        ADR     r3, SubInterrupt_Despatch
        TEQ     r2, r3
        BEQ     AlreadySubbed
        ; Need to claim the top-level interrupt.
        MOV     r3, #12
        BL      ClaimSysHeapNode
        Pull    "r5",VS
        BVS     DV_Exit
        WritePSRc SVC_mode+I_bit, r10   ; IRQs off for update (on again on SWI exit)
        LDMIA   r5, {r0, r3, r10}
        STMIA   r2, {r0, r3, r10}       ; copy current head into node
        ADR     r0, Default_SubInterruptHandler_Node
        ADR     r3, SubInterrupt_Despatch
        STR     r0, [r5, #IRQDesp_R12Val]
        STR     r3, [r5, #IRQDesp_CallAddr]
        STR     r2, [r5, #IRQDesp_Link]

AlreadySubbed
        ; r1 -> top level interrupt entry
        MOV     r3, #PodDesp_NodeSize
        BL      ClaimSysHeapNode
        Pull    "r5",VS
        BVS     DV_Exit
        MOV     r10, r2
        ADD     r1, sp, #8
        LDMFD   r1, {r1-r3}
        STR     r1, [r10, #PodDesp_CallAddr]
        STR     r2, [r10, #PodDesp_R12Val]
        STR     r3, [r10, #PodDesp_Address]
        STR     r4, [r10, #PodDesp_Mask]
        WritePSRc SVC_mode+I_bit, r2    ; IRQs off for update
        LDR     r0, [r5, #IRQDesp_R12Val]
        STR     r0, [r10, #PodDesp_Link]
        STR     r10, [r5, #IRQDesp_R12Val]
        Pull    "r5"
        B       DV_Exit
472 473
 ]
 [ CDVPoduleIRQs
Neil Turton's avatar
Neil Turton committed
474 475 476 477 478 479 480 481 482 483 484
PoduleChainClaim
        MOV     r3, #PodDesp_NodeSize
        BL      ClaimSysHeapNode
        BVS     DV_Exit
        MOV     r10, r2
        LDMFD   sp, {r0-r3}
        STR     r1, [r10, #PodDesp_CallAddr]
        STR     r2, [r10, #PodDesp_R12Val]
        STR     r3, [r10, #PodDesp_Address]
        STR     r4, [r10, #PodDesp_Mask]
        CMP     r0, #Podule_DevNo
Jeffrey Lee's avatar
Jeffrey Lee committed
485 486
        LDREQ   r0, =ZeroPage+PIRQ_Chain
        LDRNE   r0, =ZeroPage+PFIQasIRQ_Chain
Kevin Bracey's avatar
Kevin Bracey committed
487
        WritePSRc SVC_mode+I_bit, r1    ; IRQs off for update
Neil Turton's avatar
Neil Turton committed
488 489 490 491
        LDR     r1, [r0]
        STR     r1, [r10, #PodDesp_Link]
        STR     r10, [r0]
        B       DV_Exit
Kevin Bracey's avatar
Kevin Bracey committed
492
 ]
Neil Turton's avatar
Neil Turton committed
493 494 495 496 497 498 499 500 501 502 503 504 505

; .............................................................................
; Release of device vectors

; r0 = Device number
; r1 = call address
; r2 = r12 value
; r0 = PFIQ|PIRQ devno -> r3 = interrupt location (LDRB always used)
;                         r4 = interrupt mask

DeviceVector_Release ROUT

        Push    "r0-r3, lr"             ; Ensure same regset as above
Kevin Bracey's avatar
Kevin Bracey committed
506 507 508
 [ HAL
        BIC     r0, r0, #CDV_Flags
 ]
509 510 511
        LDR     lr, =ZeroPage
        LDR     lr, [lr, #IRQMax]
        CMP     r0, lr
Kevin Bracey's avatar
Kevin Bracey committed
512
        BHS     DV_Fail_NaffDevNo
Neil Turton's avatar
Neil Turton committed
513

Kevin Bracey's avatar
Kevin Bracey committed
514
        WritePSRc SVC_mode + I_bit, r12 ; IRQs off while holding context
515
 [ CDVPoduleIRQs
Neil Turton's avatar
Neil Turton committed
516 517 518
        CMP     r0, #Podule_DevNo
        CMPNE   r0, #PFIQasIRQ_DevNo
        BEQ     PoduleChainRelease
Kevin Bracey's avatar
Kevin Bracey committed
519
 ]
Neil Turton's avatar
Neil Turton committed
520 521 522 523 524 525

        ADD     r0, r0, r0, LSL #1      ; *3
        LDR     r12, =DefaultIRQ1V-DefaultIRQ1Vcode+Devices
        ADD     r12, r12, r0, LSL #2    ; address of node
        MOV     r11, #-1                ; "fudge" predecessor node

Kevin Bracey's avatar
Kevin Bracey committed
526 527 528 529 530 531 532 533
 [ HAL
;        TEQ     r3, #0
;        BNE     SubInterruptRelease
 ]

 [ HAL
01      LDMIB   r12, {r3, r10}
 |
Neil Turton's avatar
Neil Turton committed
534
01      LDMIA   r12, {r3, r10}
Kevin Bracey's avatar
Kevin Bracey committed
535
 ]
Neil Turton's avatar
Neil Turton committed
536 537 538 539
        CMP     r3, r2
        CMPEQ   r10, r1
        BEQ     %FT02                   ; found it
        MOV     r11, r12
Kevin Bracey's avatar
Kevin Bracey committed
540 541 542 543
        LDR     r12, [r12, #IRQDesp_Link]
 [ HAL
        BICS    r12, r12, #IRQDesp_Link_Unshared
 |
Neil Turton's avatar
Neil Turton committed
544
        CMP     r12, #0
Kevin Bracey's avatar
Kevin Bracey committed
545
 ]
Neil Turton's avatar
Neil Turton committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        BNE     %BT01

11      ADR     r0, ErrorBlock_BadDevVecRel
      [ International
        BL      TranslateError
      |
        SETV
      ]
        B       DV_Exit

        MakeErrorBlock BadDevVecRel


02      CMP     r11, #-1
        BEQ     %FT03
        MOV     r2, r12
Kevin Bracey's avatar
Kevin Bracey committed
562 563 564 565 566 567 568 569
        LDR     r12, [r2, #IRQDesp_Link]
 [ HAL
        LDR     r14, [r11, #IRQDesp_Link]       ; preserve r11's "unshared" flag
        BIC     r12, r12, #IRQDesp_Link_Unshared
        AND     r14, r14, #IRQDesp_Link_Unshared
        ORR     r12, r12, r14
 ]
        STR     r12, [r11, #IRQDesp_Link] ; node delinked
Neil Turton's avatar
Neil Turton committed
570 571
        B       %FT04

Kevin Bracey's avatar
Kevin Bracey committed
572
03      LDR     r2, [r12, #IRQDesp_Link]; freeable = nextnode
573 574 575
 [ HAL
        BIC     r2, r2, #IRQDesp_Link_Unshared
 ]
Neil Turton's avatar
Neil Turton committed
576 577 578 579 580 581 582 583
        LDMIA   r2,  {r0, r1, r3}       ; copy next node into head posn
        STMIA   r12, {r0, r1, r3}

04
        BL      FreeSysHeapNode         ; free block
        B       DV_Exit


Kevin Bracey's avatar
Kevin Bracey committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
 [ HAL:LAND:{FALSE}
SubInterruptRelease
        ADR     r10, SubInterrupt_Despatch
10      LDR     r0, [r12, #IRQDesp_CallAddr]
        TEQ     r0, r10
        BEQ     %FT15

13      MOV     r11, r12
        LDR     r12, [r12, #IRQDesp_Link]
        TEQ     r12, #0
        BEQ     %BT11
        B       %BT10

15      SUB     r0, r12, #PodDesp_Link
17      LDR     r14, [r0, #PodDesp_Link]
        TEQ     r14, #0
        BEQ     %BT13

        LDR     r10, [r14, #PodDesp_Address]
        CMP     r10, r3
        LDREQ   r10, [r14, #PodDesp_Mask]
        CMPEQ   r10, r4
        LDREQ   r10, [r14, #PodDesp_CallAddr]
        CMPEQ   r10, r1
        LDREQ   r10, [r14, #PodDesp_R12Val]
        CMPEQ   r10, r2
        MOVNE   r0, r14
        BNE     %BT17

        LDR     r10, [r14, #PodDesp_Link]
        STR     r10, [r0,  #PodDesp_Link]!

        LDR     r0, [r12, #IRQDesp_R12Val]
        MOV     r2, r14
        ADR     r14, Default_SubInterruptHandler_Node
        TEQ     r0, r14                 ; last sub-interrupt gone?
        BNE     %BT04

        BL      FreeSysHeapNode         ; free sub-interrupt
        BVS     DV_Exit
        B       %BT02                   ; then go back to delink top level
625 626
 ]
 [ CDVPoduleIRQs
Neil Turton's avatar
Neil Turton committed
627 628
PoduleChainRelease
        CMP     r0, #Podule_DevNo
Jeffrey Lee's avatar
Jeffrey Lee committed
629 630
        LDREQ   r0, =ZeroPage+PIRQ_Chain-PodDesp_Link
        LDRNE   r0, =ZeroPage+PFIQasIRQ_Chain-PodDesp_Link
Neil Turton's avatar
Neil Turton committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

10      LDR     r12, [r0, #PodDesp_Link]
        CMP     r12, #0
        BEQ     %BT11
        LDR     r11, [r12, #PodDesp_Address]
        CMP     r11, r3
        LDREQ   r11, [r12, #PodDesp_Mask]
        CMPEQ   r11, r4
        LDREQ   r11, [r12, #PodDesp_CallAddr]
        CMPEQ   r11, r1
        LDREQ   r11, [r12, #PodDesp_R12Val]
        CMPEQ   r11, r2
        MOVNE   r0, r12
        BNE     %BT10

        LDR     r11, [r12, #PodDesp_Link]
        STR     r11, [r0,  #PodDesp_Link]
        MOV     r2, r12
        B       %BT04
Kevin Bracey's avatar
Kevin Bracey committed
650
 ]
Neil Turton's avatar
Neil Turton committed
651 652 653 654 655 656 657 658 659 660

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Default device owner for IRQ not recognised by system: pass to IRQ2V

IRQ ROUT

        Push    "r10, lr"
        MOV     r10, #IrqV
        BL      CallVector

Kevin Bracey's avatar
Kevin Bracey committed
661
 [ HAL
Jeffrey Lee's avatar
Jeffrey Lee committed
662 663
        Pull    "r10, lr"
        Pull    "pc"                    ; new-style CDV - pull return address
Kevin Bracey's avatar
Kevin Bracey committed
664
 |
Neil Turton's avatar
Neil Turton committed
665
        Pull    "r10, pc"               ; return: someone will always claim it.
Kevin Bracey's avatar
Kevin Bracey committed
666
 ]
Neil Turton's avatar
Neil Turton committed
667 668 669

; *****************************************************************************
; Default IRQ2V:
Kevin Bracey's avatar
Kevin Bracey committed
670 671 672 673
 [ HAL
;   r0  must still have devno in it
;   r12 is 0 (from vector)
 |
Neil Turton's avatar
Neil Turton committed
674 675
;   r0  must still have devno*3 in it
;   r12 is 0 (from vector)
Kevin Bracey's avatar
Kevin Bracey committed
676
 ]
Neil Turton's avatar
Neil Turton committed
677 678 679 680 681 682 683

; Clear mask, clear IRQ as appropriate/possible

; NB. a cheap way of dividing by ~3 is *11,/32: accurate for 0..31 result ...

NOIRQ ROUT

Kevin Bracey's avatar
Kevin Bracey committed
684
 [ HAL
Kevin Bracey's avatar
Kevin Bracey committed
685 686
        TEQ     r0, #0
        Pull    pc, MI
Kevin Bracey's avatar
Kevin Bracey committed
687 688 689 690 691
        MOV     r11, r9
        AddressHAL
        CallHAL HAL_IRQDisable
        MOV     r9, r11
 |
Neil Turton's avatar
Neil Turton committed
692 693 694 695
01      SUBS    r0, r0, #3
        ADDGE   r12, r12, #1
        BGT     %BT01                   ; r12 := r0 DIV 3

Kevin Bracey's avatar
Kevin Bracey committed
696 697 698
        CMP     R12, #8
        MOVLO   R0, #IOCIRQMSKA
        BLO     %FT03
Neil Turton's avatar
Neil Turton committed
699

Kevin Bracey's avatar
Kevin Bracey committed
700 701 702 703
        CMP     R12, #16
        SUBLO   R12, R12, #8
        MOVLO   R0, #IOCIRQMSKB
        BLO     %FT03
Neil Turton's avatar
Neil Turton committed
704

Kevin Bracey's avatar
Kevin Bracey committed
705 706 707 708
        CMP     R12, #IOMD_MouseRxFull_DevNo
        SUBLO   R12, R12, #IOMD_DMAChannel0_DevNo
        MOVLO   R0, #IOMD_DMAMSK
        BLO     %FT03
Neil Turton's avatar
Neil Turton committed
709

Kevin Bracey's avatar
Kevin Bracey committed
710 711 712 713
 [ MorrisSupport
        CMP     R12, #IOMD_C_Bit0_DevNo
        SUBLO   R12, R12, #IOMD_MouseRxFull_DevNo       ;reduce to bit number 0..7
        MOVLO   R0, #IOMD_IRQMSKD                       ; in IRQ D interrupt register
Neil Turton's avatar
Neil Turton committed
714

Kevin Bracey's avatar
Kevin Bracey committed
715 716
        SUBHS   R12, R12, #IOMD_C_Bit0_DevNo
        MOVHS   R0, #IOMD_IRQMSKC
Neil Turton's avatar
Neil Turton committed
717 718 719 720 721 722 723
 ]

03
        ADD     r0, r0, #IOC
        MOV     r1, #1
        MOV     r1, r1, LSL r12         ; bit to clear

724
        MRS     lr, CPSR
Kevin Bracey's avatar
Kevin Bracey committed
725 726
        BIC     r12, lr, #&0F
        ORR     r12, r12, #I32_bit+F32_bit+IRQ_mode
727
        MSR     CPSR_c, r12
Neil Turton's avatar
Neil Turton committed
728 729 730
        LDRB    r12, [r0]       ; FIQs off for updating IOCIRQMSKA
        BIC     r12, r12, r1
        STRB    r12, [r0]       ; relevant IRQ disabled
731
        MSR     CPSR_c, lr      ; absolute minimum FIQ disable period
Neil Turton's avatar
Neil Turton committed
732 733

        STRB    r1, [r0, #IOCIRQCLRA-IOCIRQMSKA] ; Clear IRQ
Kevin Bracey's avatar
Kevin Bracey committed
734
 ]
Kevin Bracey's avatar
Kevin Bracey committed
735
        Pull    pc              ; claim vector
Neil Turton's avatar
Neil Turton committed
736 737 738 739 740 741 742 743 744 745

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; The following bits have been appropriated from source.pmf.oseven to make
; sure Tim's old code doesn't overwrite us when he gets back!

; SWI OS_GenerateEvent: call event vector if enabled

GenEvent ROUT

        Push    lr
Kevin Bracey's avatar
Kevin Bracey committed
746
        WritePSRc SVC_mode+I_bit, lr    ; Disable IRQs. MUST call these ones
Neil Turton's avatar
Neil Turton committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
        BL      OSEVEN                  ; in SVC mode as people expect it
        Pull    lr
        B       SLVK

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Subroutine call version

; In    r0 = event type
;       r1,r2 parameters

; Out   C=0 => event was enabled, or was >= 32 anyway
;       C=1 => event was disabled, so vector not called

OSEVEN ROUT

        Push    lr

        CMP     r0, #31                 ; Events >= 32 are ALWAYS raised. SKS
                                        ; flags are HI if so, ie. NE
Jeffrey Lee's avatar
Jeffrey Lee committed
766
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
767
        LDRLSB  r14, [r0, #OsbyteVars + :INDEX: EventSemaphores]
Jeffrey Lee's avatar
Jeffrey Lee committed
768 769 770 771
      |
        LDRLS   r14, =ZeroPage+OsbyteVars+:INDEX:EventSemaphores
        LDRLSB  r14, [r0, r14]
      ]
Neil Turton's avatar
Neil Turton committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
                                        ; get semaphore for this event 0..31
        CMPLS   r14, #0                 ; non-zero => enabled
        Pull    pc, EQ                  ; if disabled, exit with C=1

        Push    "r0-r3, r10-r12" ; r3 excessive ???
        MOV     r10, #EventV            ; call event vector
        BL      CallVector
        CLC                             ; indicate event enabled
        Pull    "r0-r3, r10-r12, pc"

; ...................... default owner of EventV ..............................
; Call Event handler

; In    r12 = EvtHan_ws

DefEvent ROUT

        MOV     lr, pc                  ; link with all the bits
        LDMIA   r12, {r12, pc}          ; call EventHandler, returns to ...

        TEQ     r12, #1
Kevin Bracey's avatar
Kevin Bracey committed
793
        Pull    pc,NE
Neil Turton's avatar
Neil Turton committed
794

Jeffrey Lee's avatar
Jeffrey Lee committed
795
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
796 797 798
        LDRB    r14, [r12, #CallBack_Flag-1] ; IRQs are still disabled
        ORR     r14, r14, #CBack_OldStyle
        STRB    r14, [r12, #CallBack_Flag-1]
Jeffrey Lee's avatar
Jeffrey Lee committed
799 800 801 802 803 804 805
      |
        LDR     r12, =ZeroPage
        LDRB    r14, [r12, #CallBack_Flag] ; IRQs are still disabled
        ORR     r14, r14, #CBack_OldStyle
        STRB    r14, [r12, #CallBack_Flag]
      ]

Kevin Bracey's avatar
Kevin Bracey committed
806
        Pull    pc                      ; claim EventV
Neil Turton's avatar
Neil Turton committed
807 808 809 810 811

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Process timer zero IRQ device (100Hz clock)
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Neil Turton's avatar
Neil Turton committed
812 813
        ALIGN 32

Neil Turton's avatar
Neil Turton committed
814 815
TickOne ROUT

816
 [ HAL
Kevin Bracey's avatar
Kevin Bracey committed
817
        ; Don't push r14 - we're using new interface, and claim the vector
818
        Push    "r0,r9,r12"
819
        AddressHAL
820 821 822 823

        ; In some chips, Timer0 is latched in the timer block instead of,
        ; or as well as in the interrupt controller
        MOV     R0, #0 ; clear latch for Timer0
824
        CallHAL HAL_TimerIRQClear
825 826

        Pull    "r0" ; get device number back
827
        CallHAL HAL_IRQClear
Kevin Bracey's avatar
Kevin Bracey committed
828
        Pull    "r9,r12"
829
 |
Kevin Bracey's avatar
Kevin Bracey committed
830 831
        Push    r14

Neil Turton's avatar
Neil Turton committed
832 833
        MOV     R0, #timer0_bit
        STRB    R0, [R3, #IOCIRQCLRA]   ; clear timer 0 interrupt
834
 ]
Neil Turton's avatar
Neil Turton committed
835

Jeffrey Lee's avatar
Jeffrey Lee committed
836
        LDR     R1, =ZeroPage
837 838 839 840
        LDR     R0, [R1, #MetroGnome]
        ADD     R0, R0, #1
        STR     R0, [R1, #MetroGnome]

Neil Turton's avatar
Neil Turton committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
        LDRB    R0, CentiCounter        ; Counter for VDU CTRL timing
        SUBS    R0, R0, #1
        STRCSB  R0, CentiCounter        ; decrement if not zero

        LDR     R0, IntervalTimer +0
        ADDS    R0, R0, #1              ; Increment the low 4 bytes
        STR     R0, IntervalTimer +0

        LDREQB  R0, IntervalTimer +4
        ADDEQ   R0, R0, #1              ; and carry into 5th byte if necessary
        STREQB  R0, IntervalTimer +4

        Push    "R4,R12"                ; R0-R3 already pushed

        TEQEQ   R0, #&100               ; has interval timer crossed zero ?
        MOVEQ   R0, #Event_IntervalTimer ; Event ITCZ
        BLEQ    OSEVEN

        BL      CentiSecondTick         ; Notify keyboard of a centisecond

        Pull    "R4,R12"

        LDR     R0, RealTime +0         ; Increment 5-byte real time
        ADDS    R0, R0, #1
        STR     R0, RealTime +0
        LDRCSB  R0, RealTime +4
        ADDCS   R0, R0, #1              ; Won't wrap until 2248 and then it
        STRCSB  R0, RealTime +4         ; all falls over anyway

        LDRB    R0, TimerState          ; get switch state
        TEQ     R0, #5                  ; toggles between 5 and 10

        LDREQ   R1, TimerAlpha +0       ; either load from one
        LDREQB  R2, TimerAlpha +4

        LDRNE   R1, TimerBeta +0        ; or the other
        LDRNEB  R2, TimerBeta +4

        ADREQ   R3, TimerBeta +0        ; and point to t'other
        ADRNE   R3, TimerAlpha +0

        ADDS    R1, R1, #1              ; increment
        ADC     R2, R2, #0              ; with carry

        STR     R1, [R3]                ; and store back
        STRB    R2, [R3, #4]

        EOR     R0, R0, #&0F            ; 5 <-> 10
        STRB    R0, TimerState

        Push    R10

 [ TickIrqReenter
        MOV     R10, #TickerV           ; call 100Hz vector
        BL      CallVector              ; IRQ's still disabled

        BL      ProcessTickEventChain   ; Re-enables IRQs
 |
        BL      ProcessTickEventChain

        MOV     R10, #TickerV           ; call 100Hz vector
        BL      CallVector
 ]
        Pull    "R10,PC"

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Process VSync IRQ device
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Neil Turton's avatar
Neil Turton committed
910
        ALIGN 32
John Ballance's avatar
John Ballance committed
911
FalseVsyncIRQ ROUT
Jeffrey Lee's avatar
Jeffrey Lee committed
912
        LDR     R1, =ZeroPage
John Ballance's avatar
John Ballance committed
913 914 915 916 917
        LDR     R0, [R1, #MetroGnome]
        TST     R0, #1
        MOVEQ   pc, lr
        Push    "lr"
        B       VsyncIRQ_ExtEntry
Neil Turton's avatar
Neil Turton committed
918

Neil Turton's avatar
Neil Turton committed
919 920
VsyncIRQ ROUT

Kevin Bracey's avatar
Kevin Bracey committed
921
    [ HAL
Kevin Bracey's avatar
Kevin Bracey committed
922
        ; Don't push r14 - we're using new interface, and claim the vector
Kevin Bracey's avatar
Kevin Bracey committed
923
        Push    "r9,r12"
Kevin Bracey's avatar
Kevin Bracey committed
924 925
        AddressHAL
        CallHAL HAL_IRQClear
Kevin Bracey's avatar
Kevin Bracey committed
926
        Pull    "r9,r12"
Kevin Bracey's avatar
Kevin Bracey committed
927
    |
Kevin Bracey's avatar
Kevin Bracey committed
928 929
        Push    r14

Neil Turton's avatar
Neil Turton committed
930 931
        MOV     R0, #vsync_bit
        STRB    R0, [R3, #IOCIRQCLRA]   ; Clear the vsync interrupt
Kevin Bracey's avatar
Kevin Bracey committed
932
    ]
Neil Turton's avatar
Neil Turton committed
933

Kevin Bracey's avatar
Kevin Bracey committed
934
VsyncIRQ_ExtEntry
Neil Turton's avatar
Neil Turton committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
        LDRB    R0, CFStime             ; decrement 'CFS' timer !
        SUB     R0, R0, #1
        STRB    R0, CFStime

        VDWS    WsPtr                   ; Do our stuff before issuing VSYNC event
        BL      VsyncCall
        BYTEWS  WsPtr

        MOV     R0, #Event_VSync        ; VSYNC event number
        BL      OSEVEN

        LDRB    R1, FlashCount
        SUBS    R1, R1, #1
        Pull    PC, CC                  ; was zero, so frozen
        STRNEB  R1, FlashCount          ; else if now non-zero, store it back
        Pull    PC, NE                  ; not time to flash yet

        LDRB    R1, FlashState          ; Get the state and
        EORS    R1, R1, #1              ; flip to the other one (setting flags)
        STRB    R1, FlashState

        LDREQB  R2, SpacPeriod          ; get appropriate new period
        LDRNEB  R2, MarkPeriod
        STRB    R2, FlashCount          ; and put into counter

        VDWS    WsPtr
        Push    R4
        BEQ     dothesecondflash

dothefirstflash
        BL      DoFirstFlash
        Pull    "R4, PC"

dothesecondflash
        BL      DoSecondFlash
        Pull    "R4, PC"

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        END