Page 1 of 1

RTC Problems

Posted: Sat Dec 12, 2020 5:28 pm
by Erukaron
Hi,

i have problems with configuring the rtc (bochs 2.6.11). I thought i may be doing something that causes problems when working with the rtc, so to eliminate every possible conflict, i just have a bare minimum bootable code, that sets the interrupt and should enable the rtc periodic interrupt (according to https://wiki.osdev.org/RTC and https://www.compuphase.com/int70.txt).

The entire code is listed below.

Code: Select all

bits 16
org 0x7c00
jmp start
;--------------------------------------------------------------------------------------

;--------------------------------------------------------------------------------------
; Loader
;--------------------------------------------------------------------------------------
start:
    cli 

    mov ax, 0x0000
    mov ds, ax
    mov es, ax

    mov ax, 0x1000
    mov ss, ax
    xor sp, sp

    ; Set up ivt for 0x70 -> RTC
    push ds
        mov dx, 0x0000
        mov ds, dx

        ; each ivt entry is 32 bit long [offset][segment]
        mov cl, 4
        mov ah, 0
        mov al, 0x70
        mul cl

        mov si, ax

        mov [ds:si], word interrupt_called
        mov [ds:si + 2], cs
    pop ds

    ; Test rtc
    xchg bx, bx

    cli

    ; Turn on irq 8
    mov al, 0x8b 
    out 0x70, al ; select register b and disable NMI

    in al, 0x71 ; read current content of register b
    mov ah, al

    mov al, 0x8b
    out 0x70, al ; reset register b

    or ah, 0x40
    mov al, ah
    out 0x71, al ; Enable periodic interrupt

    ; read register c to allow for new interrupts
    mov al, 0x0c
    out 0x70, al
    in al, 0x71

    ; Also set to register d according to https://www.compuphase.com/int70.txt
    mov al, 0x0d
    out 0x70, al
    in al, 0x71

    sti

    .wait_for_int:
        nop
    jmp .wait_for_int
;--------------------------------------------------------------------------------------

interrupt_called:
    xchg bx, bx
    iret

;--------------------------------------------------------------------------------------
; Boot-Magic
;--------------------------------------------------------------------------------------
times 510-($-$$) nop
db 0x55
db 0xaa
;--------------------------------------------------------------------------------------
The problem is, i am stuck in the "wait_for_int" loop. To my understanding "interrupt_called" should be reached at least once, but why isn't it?

Re: RTC Problems

Posted: Sat Dec 12, 2020 7:53 pm
by Octocontrabass
Erukaron wrote:

Code: Select all

        mov cl, 4
        mov ah, 0
        mov al, 0x70
        mul cl
Why are you calculating a constant your assembler can calculate for you? For example: "mov si, 0x70 * 4".
Erukaron wrote:

Code: Select all

        mov [ds:si + 2], cs
You're assuming CS contains a sensible value, but in a boot sector, that isn't guaranteed. If you really want to use CS, set it to a reasonable value first (e.g. using a far JMP). In this case, the value you want to use will always be 0, so you can skip setting CS and directly store 0 to memory.
Erukaron wrote:To my understanding "interrupt_called" should be reached at least once, but why isn't it?
Did you unmask IRQ8 in the interrupt controllers?

Re: RTC Problems

Posted: Sun Dec 13, 2020 1:58 am
by Erukaron
Thank you for your reply.
Octocontrabass wrote: Why are you calculating a constant your assembler can calculate for you? For example: "mov si, 0x70 * 4".
Yes, i am aware of that. This code is copied from a routine in the kernel that calculates the position for a given interrupt in the ivt.
Octocontrabass wrote: You're assuming CS contains a sensible value, but in a boot sector, that isn't guaranteed. If you really want to use CS, set it to a reasonable value first (e.g. using a far JMP). In this case, the value you want to use will always be 0, so you can skip setting CS and directly store 0 to memory.
This is also a fragment from the before mentioned kernel routine to set interrupts. And was just copied as is.
Octocontrabass wrote: Did you unmask IRQ8 in the interrupt controllers?
Thank you, that was exactly the piece i was missing. It works fine now. Have a nice day!