Skip to content

Commit

Permalink
Adding support for Interrupts.
Browse files Browse the repository at this point in the history
Squash-merging changes by @tanay-man.

Thanks!
  • Loading branch information
tanay-man authored May 28, 2024
1 parent 3f898c5 commit 287902c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2821,6 +2821,20 @@ void cpu_exec(cpu *c, u8 opcode) {
(c->ip)+=2;
break;

case 0xcd:
u8 operand = cpu_fetch(c);
printf("Interrupt called with argument %d",operand);
interrupt(c,operand);
break;

case 0xcc:
interrupt(c,3);
break;

case 0xcf:
iret(c);
break;

default: break; /* nops and unused */
}
/* setting the segment override to 0 after executing every instruction */
Expand Down
2 changes: 1 addition & 1 deletion src/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "jumps.h"
#include "mul_div.h"
#include "push_pop.h"

#include "interrupts.h"
/* cpu procs */
cpu* cpu_make();
void cpu_init(cpu* c);
Expand Down
20 changes: 20 additions & 0 deletions src/interrupts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "interrupts.h"

void interrupt(cpu* c, u8 interrupt_type){
push16(c,c->flags);
resetTF(c);
resetIF(c);
push_r(c,c->cs);
push_r(c,c->ip);
u32 interrupt_pointer = interrupt_type*4;
c->ip = cpu_read_u16_at(c,interrupt_pointer);
c->cs = cpu_read_u16_at(c,interrupt_pointer+2);
return;
}

void iret(cpu* c){
c->ip = pop16(c);
c->cs = pop16(c);
c->flags = pop16(c);
return;
}
23 changes: 23 additions & 0 deletions src/interrupts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#ifndef INT_H
#define INT_H
#include "push_pop.h"
#include "flagops.h"
#include "cpu.h"
// case 0x90 NOP No operation

// case 0xcd INT interrupt
// case 0xce INTO interrupt on overflow
// case 0xcf IRET interrupt return

// case 0xf4 HLT halt
// case 0xfa CLI clear if ---> Already implemented
// case 0xfb STI set if ---> Already implemented

void interrupt(cpu* c,u8 interrupt_type);
void into(void);
void iret(cpu* c);
void halt(void);


#endif

0 comments on commit 287902c

Please sign in to comment.