Skip to content

Commit 7d7c367

Browse files
committed
Reorganize PIC and PIT driver
1 parent c615f03 commit 7d7c367

File tree

12 files changed

+200
-227
lines changed

12 files changed

+200
-227
lines changed

.gdbinit

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
set $CS_BASE = 0x20000
2-
set $DS_BASE = 0x20000
3-
set $SS_BASE = 0x20000
1+
set $CS_BASE = 0x30000
2+
set $DS_BASE = 0x30000
3+
set $SS_BASE = 0x30000
44

55
# config
66
set architecture i386:x86-64
@@ -72,17 +72,17 @@ define view_realmode
7272
end
7373

7474
define view_kernelmode
75-
set $CS_BASE = 0xC000
76-
set $DS_BASE = 0xC000
77-
set $SS_BASE = 0xC000
75+
set $CS_BASE = 0x10000
76+
set $DS_BASE = 0x10000
77+
set $SS_BASE = 0x10000
7878
symbol-file build/kernel/core.elf
7979
clayout
8080
end
8181

8282
define view_usermode
83-
set $CS_BASE = 0x20000
84-
set $DS_BASE = 0x20000
85-
set $SS_BASE = 0x20000
83+
set $CS_BASE = 0x30000
84+
set $DS_BASE = 0x30000
85+
set $SS_BASE = 0x30000
8686
symbol-file
8787
clayout
8888
end

include/fuzzy/drivers/pic/pic.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#pragma once
22

3+
#include <stddef.h>
4+
35
#define PIC_PIT_FREQ 1193182 // hz
46
#define PIC_PIT_MAX_COUNTER 0xFFFF
57

68
#define PIC_IRQ_PIT 0
79

8-
void interrupt_pit_enable();
10+
void pic_init();
11+
12+
void pic_writemask_new(uint16_t mask);
13+
uint16_t pic_readmask_new();
914

1015
void pic_irq_enable(int irq);
1116
void pic_irq_disable(int irq);
12-
13-
void pic_pit_set_counter(unsigned short counter);
14-
unsigned short pic_pit_get_counter();
15-
void pic_pit_reset();

include/fuzzy/drivers/pic/pit.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
5+
static uint16_t frequency_dividor;
6+
7+
void pit_init();
8+
9+
void pit_set_counter(uint16_t counter);
10+
uint16_t pit_get_counter();
11+
12+
void pit_reload_counter();
13+
void pit_reset();

include/fuzzy/drivers/port.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
5+
#define PORT_PIC1_CMD 0x20
6+
#define PORT_PIC1_DATA 0x21
7+
#define PORT_PIC2_CMD 0xA0
8+
#define PORT_PIC2_DATA 0xA1
9+
10+
#define PORT_PIT_DATA0 0x40
11+
#define PORT_PIT_DATA1 0x41
12+
#define PORT_PIT_DATA2 0x42
13+
#define PORT_PIT_CMD 0x43
14+
15+
static inline void outb(uint16_t port, uint8_t data) {
16+
__asm__ volatile(
17+
"outb %0, %1 \n"
18+
: /* output */
19+
: "a" (data), "ir" (port) /* input */
20+
:
21+
);
22+
}
23+
24+
static inline uint8_t inputb(uint16_t port) {
25+
uint8_t data;
26+
__asm__ volatile(
27+
"inb %1, %0 \n"
28+
: "=a" (data) /* output */
29+
: "ir" (port) /* input */
30+
:
31+
);
32+
return data;
33+
}

src/drivers/pic/Makefile.mk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@ $(SELF_BUILD_DIR)/%.o: $(SELF_SRC_DIR)/%.c $(BUILD_USR_INCLUDE_ALL)
22
mkdir -p $(dir $@)
33
$(KERNEL_CC) -c -o $@ $<
44

5-
$(SELF_BUILD_DIR)/%_asm.o: $(SELF_SRC_DIR)/%.asm
6-
mkdir -p $(dir $@)
7-
nasm -o $@ -f elf32 $<
8-
95
$(SELF_BUILD_DIR)/libpic: $(SELF_BUILD_ALL_C) $(SELF_BUILD_ALL_ASM)
106
ar rc $@ $^

src/drivers/pic/pic.asm

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/drivers/pic/pic.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,52 @@
1+
#include <fuzzy/drivers/port.h>
12
#include <fuzzy/drivers/pic/pic.h>
23
#include <fuzzy/kernel/interrupts/interrupts.h>
34
#include <lib/utils/logging.h>
45

5-
extern void _pic_init_low(int idt_irq0_pic1, int idt_irq0_pic2);
6-
extern void _pic_pit_init();
6+
void pic_init() {
7+
print_log("[driver][pic] init");
8+
int idt_irq0_pic1 = IDT_IRQ_OFFSET;
9+
int idt_irq0_pic2 = IDT_IRQ_OFFSET+8;
710

8-
extern void pic_timer_set_counter(unsigned short counter);
9-
extern unsigned short pic_timer_get_counter();
10-
extern void pic_pit_reset();
11+
outb(PORT_PIC1_CMD, 0x11);
12+
outb(PORT_PIC2_CMD, 0x11);
1113

12-
extern unsigned short _pic_readmask();
13-
extern void _pic_writemask(unsigned short mask);
14+
// Remap IRQ
15+
// FuzzyOS won't be using IRQ0 in real mode.
16+
outb(PORT_PIC1_DATA, idt_irq0_pic1);
17+
outb(PORT_PIC2_DATA, idt_irq0_pic2);
1418

15-
void pic_init() {
16-
print_log("PIC init.");
17-
_pic_init_low(IDT_IRQ_OFFSET, IDT_IRQ_OFFSET+8);
19+
outb(PORT_PIC1_DATA, 4);
20+
outb(PORT_PIC2_DATA, 2);
21+
22+
outb(PORT_PIC1_DATA, 1);
23+
outb(PORT_PIC2_DATA, 1);
1824

1925
// disable all IRQs
20-
_pic_writemask(0xFFFF);
26+
pic_writemask_new(0xFFFF);
27+
28+
pit_init();
29+
}
30+
31+
void pic_writemask_new(uint16_t mask) {
32+
outb(PORT_PIC1_DATA, mask&0xFF);
33+
outb(PORT_PIC2_DATA, mask>>8);
34+
}
2135

22-
_pic_pit_init();
36+
uint16_t pic_readmask_new() {
37+
uint16_t mask = inputb(PORT_PIC1_DATA);
38+
mask |= inputb(PORT_PIC2_DATA) << 8;
39+
return mask;
2340
}
2441

2542
void pic_irq_enable(int irq) {
26-
int mask = _pic_readmask();
43+
int mask = pic_readmask_new();
2744
mask = mask&(~(1<<irq));
28-
_pic_writemask(mask);
45+
pic_writemask_new(mask);
2946
}
3047

3148
void pic_irq_disable(int irq) {
32-
int mask = _pic_readmask();
49+
int mask = pic_readmask_new();
3350
mask = mask|((1<<irq));
34-
_pic_writemask(mask);
35-
}
51+
pic_writemask_new(mask);
52+
}

src/drivers/pic/pit.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <fuzzy/drivers/port.h>
2+
#include <fuzzy/drivers/pic/pit.h>
3+
4+
static uint16_t frequency_dividor;
5+
6+
void pit_init() {
7+
// Channel 0
8+
// Low/high byte
9+
// Interrupt on terminal count
10+
outb(PORT_PIT_CMD, 0b00110000);
11+
}
12+
13+
uint16_t pit_get_counter() {
14+
return frequency_dividor;
15+
}
16+
17+
void pit_reload_counter() {
18+
uint16_t counter = pit_get_counter();
19+
outb(PORT_PIT_DATA0, counter&0xFF);
20+
outb(PORT_PIT_DATA0, counter>>8);
21+
}
22+
23+
24+
void pit_reset() {
25+
outb(PORT_PIC1_CMD, 0x20);
26+
outb(PORT_PIC2_CMD, 0x20);
27+
pit_reload_counter();
28+
}
29+
30+
void pit_set_counter(uint16_t counter) {
31+
frequency_dividor = counter;
32+
pit_reload_counter();
33+
}

src/kernel/interrupts/timer.asm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
global irq0_pit_handler_low
66
extern irq0_pit_handler
7-
extern pic_pit_reset
7+
extern pit_reset
88

99
global create_infant_process_irq0_stack
1010

@@ -79,7 +79,7 @@ global create_infant_process_irq0_stack
7979
irq0_pit_handler_low:
8080
_int_irq0_start
8181
call irq0_pit_handler
82-
call pic_pit_reset
82+
call pit_reset
8383
_int_irq0_end
8484
iret
8585

src/kernel/interrupts/timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int get_time_since_boot_ms() {
3434
void irq0_pit_handler(int *e_ip, int *e_cs, int *e_sp, int *e_ss) {
3535
// called every X milli seconds.
3636
// time period is defined by pic_pit_set_counter.
37-
unsigned short ticks_jumped = pic_pit_get_counter();
37+
unsigned short ticks_jumped = pit_get_counter();
3838

3939
int oldtime_ms = get_time_since_boot_ms();
4040
timer_add_ticks(ticks_jumped);
@@ -44,7 +44,7 @@ void irq0_pit_handler(int *e_ip, int *e_cs, int *e_sp, int *e_ss) {
4444
}
4545

4646
void interrupt_pit_enable() {
47-
pic_pit_set_counter(PIC_PIT_FREQ/100); // 10ms
47+
pit_set_counter(PIC_PIT_FREQ/100); // 10ms
4848
pic_irq_enable(PIC_IRQ_PIT);
4949
}
5050

0 commit comments

Comments
 (0)