Page 1 of 1

IRQ ERROR

Posted: Mon Jan 31, 2022 3:19 am
by ekremkocak
The procedure does not run when a key is pressed on the keyboard

Code: Select all

procedure Main(); stdcall;
begin
  cls(0);
  WriteStr(0,0,'Hello World',4);
  init_idt;
	init_pic;
	init_timer(100);

  asm
    sti
  end;

  LIST('/'); // <----- Floppy Read List ok
  FloppyReset(0);

  While True do
  begin
  end;

end;

procedure keyboard_callback();
var
 Scancode:byte;
begin
  if (ReadPortB($64) and $1)=1 then
   begin
     Scancode:=ReadPortB($60);
     case Scancode of
     2:begin
       LIST('/');  // <----- Floppy Read List error
      end;

   end;
end;

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 3:30 am
by ekremkocak

Code: Select all

const
	PIC1 = $20;
	PIC2 = $A0;
	ICW1 = $11;
	ICW4 = $01;
	
	
type

 TDWORD = record
   case Byte of
   0:(Value32:DWORD);
   1:(LoWord, HiWord: WORD);
 end;

TIDTEntry = packed record
		base_low  : WORD;
                selector  : WORD;
                always_0  : BYTE;
                flags     : BYTE;
                base_high : WORD;
	end;

	TIDTPtr = packed record
		limit : word;
		base : LongWord;
	end;

var
	IDTTables : array[0..255] of TIDTEntry;
	IDTPtr : TIDTPtr;

procedure setup_int(Number : Byte; Base : TDWord; Selector : Word; Flags : Byte); forward;
procedure init_idt; forward;
procedure init_pic; forward;


procedure keys_isr(); register; assembler;
asm
  pushad
  push gs
  push fs
  push es
  push ds

  
  call keyboard_callback

  mov dx, $20
  mov al, $20
  out dx, al

  pop ds
  pop es
  pop fs
  pop gs
  popad
  iretd
end;

procedure timer_isr(); register; assembler;
asm
  pushad
  push gs
  push fs
  push es
  push ds


  call TimerHandler

  mov dx, $20
  mov al, $20
  out dx, al

  pop ds
  pop es
  pop fs
  pop gs
  popad
  iretd
end;


procedure floppy_isr(); register; assembler;
asm
  pushad
  push gs
  push fs
  push es
  push ds


  call FloppyHandler

  mov dx, $20
  mov al, $20
  out dx, al

  pop ds
  pop es
  pop fs
  pop gs
  popad
  iretd

 end;

procedure  unknown_interrupt_isr(); register; assembler;
asm
  pushad
  push gs
  push fs
  push es
  push ds

  mov dx, $20
  mov al, $20
  out dx, al 

  pop ds
  pop es
  pop fs
  pop gs
  popad
  iretd
end;

procedure setup_int(Number : Byte; Base : TDWord; Selector : Word; Flags : Byte);
begin
         IDTTables[Number].base_high:= Base.HiWord;
         IDTTables[Number].base_low:= (Base.LoWord );        //  and $0000FFFF
         IDTTables[Number].selector:= Selector;
         IDTTables[Number].flags:= Flags;
         IDTTables[Number].always_0:= $00;
end;

procedure Init_idt;
var
	i:integer;
begin
	for i:=0 to 255 do
	begin
		setup_int(i, TDWord(@unknown_interrupt_isr), $08,$8E);
	end;
	setup_int(32, TDWORD(@timer_isr), $08,$8E);
	setup_int(33, TDWORD(@keys_isr), $08,$8E);
	setup_int(38, TDWORD(@floppy_isr), $08,$8E);
	IDTPtr.limit := (SizeOf(TIDTEntry)*256)-1;
	IDTPtr.base := DWord(@IDTTables);
	asm
		lidt [IDTPtr]
  end;
end;

procedure init_pic;
begin
	WritePortB(PIC1, ICW1);
	WritePortB(PIC2, ICW1);
	WritePortB(PIC1 + 1, pic1);
	WritePortB(PIC2 + 1, pic2);
	WritePortB(PIC1 + 1, 4);
	WritePortB(PIC2 + 1, 2);
	WritePortB(PIC1 + 1, ICW4);
	WritePortB(PIC2 + 1, ICW4);
	WritePortB(PIC1 + 1, 0);
	WritePortB(PIC2 + 1, 0);
end;

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 10:30 am
by Klakap
Are you in real or in protected mode? If you use GRUB, did you already set your own GDT table?

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 10:42 am
by ekremkocak
GDT ok, the program works fine on qemu, virtualbox and real pc. but when I press a key on the keyboard, normal functions are ok but IRQ0 time delay or floppy IRQ6 LIST('/'); not working.

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 10:47 am
by ekremkocak
While do <<<---- OK

Code: Select all

procedure Main(); stdcall;
begin
  cls(0);
  WriteStr(0,0,'Hello World',4);
  init_idt;
 //	init_pic;
	init_timer(100);

  asm
    sti
  end;

  LIST('/'); // <----- List ok
  FloppyReset(0);

  While True do
  begin
     keyboard_callback();   //<----   List OK
  end;

end;

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 10:51 am
by ekremkocak
IRQ1
CLS PRINT OK
DELAY, LIST ERROR (IRQ0 IRQ6)

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 10:53 am
by Octocontrabass
If you run your code in QEMU with "-d int", which interrupts appear in the log?

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 12:24 pm
by ekremkocak
SMM: enter
EAX=00000001 EBX=80000070 ECX=00000cf8 EDX=000000b3
ESI=02000000 EDI=0009fb8c EBP=0009fb24 ESP=0009fb14
EIP=000e0aa0 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0010 00000000 ffffffff 00cf9b00 DPL=0 CS32 [-RA]
SS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0000 00000000 00000000 00000000
GS =0000 00000000 00000000 00000000
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 000fb867 00000030
IDT= 000f0000 00000000
CR0=60000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=0000002d CCD=00000001 CCO=LOGICB
SMM: after RSM
EAX=00000001 EBX=80000070 ECX=00000cf8 EDX=000000b3
ESI=02000000 EDI=0009fb8c EBP=0009fb24 ESP=0009fb14
EIP=000e0aa0 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0018 00000000 ffffffff 00c09300 DPL=0 DS [-WA]
CS =0010 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0018 00000000 ffffffff 00c09300 DPL=0 DS [-WA]
DS =0018 00000000 ffffffff 00c09300 DPL=0 DS [-WA]
FS =0000 00000000 00000000 00000000
GS =0000 00000000 00000000 00000000
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 000fb867 00000030
IDT= 000f0000 00000000
CR0=60000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=ffffff9c CCO=EFLAGS
0: v=21 e=0000 i=0 cpl=0 IP=0008:0014db70 pc=0014db70 SP=0010:00067a78 EAX=0000002e
EAX=0000002e EBX=00034820 ECX=00402e00 EDX=00000040
ESI=0003499b EDI=00000000 EBP=00067a9c ESP=00067a78
EIP=0014db70 EFL=00000202 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 00163044 00001fff
IDT= 0016283c 000007ff

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 12:43 pm
by Octocontrabass
Looks like you're receiving at least one keyboard interrupt. Where's the rest of the log?

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 1:29 pm
by ekremkocak
qemu log file

Re: IRQ ERROR

Posted: Mon Jan 31, 2022 1:50 pm
by Octocontrabass
There are floppy disk and keyboard IRQs in your log, so those seem to be working fine. Which key are you pressing? It looks like your keyboard handler only responds to one specific scan code.

I don't see any timer IRQs. How are you setting up the timer?

Re: IRQ ERROR

Posted: Wed Feb 02, 2022 3:14 am
by ekremkocak
When a function is called from IRQ1 on IRQ6 or IRQ0, it goes into an infinite loop.

procedure IRQ6wait;
begin
while FloppyDiskIRQ = true do ; <--- error
end;

procedure IRQ0Delay(Ticks: LongWord);
var
ETicks: LongWord;
begin
ETicks:=TimerTicks+Ticks;
while TimerTicks<ETicks do ; <----- error
end;

Re: IRQ ERROR

Posted: Wed Feb 02, 2022 11:14 am
by Octocontrabass
Does your compiler know those variables are changed by the IRQ handler? By default, the compiler assumes interrupt handlers will not modify any variables, so the loops you wrote can be optimized into infinite loops.

I'm not very familiar with Pascal, but I think some compilers support a "volatile" keyword that works similar to "volatile" in C. Your compiler's manual should explain it in more detail.

Also, according to the QEMU log you posted earlier, the timer is not raising any IRQs. You may not be initializing the timer correctly.