Skip to content

Commit

Permalink
- fix #120
Browse files Browse the repository at this point in the history
- adding date to time display

Signed-off-by: ᛒᛅᚱᚾᚢᛚᚠ <[email protected]>
  • Loading branch information
root authored and root committed Aug 24, 2024
1 parent 01fab8a commit 713bb3d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 97 deletions.
160 changes: 70 additions & 90 deletions src/arch/i386/kernel/drivers/pit.c
Original file line number Diff line number Diff line change
@@ -1,112 +1,92 @@
#include "../include/drivers/pit.h"
#include "../include/drivers/pic.h"
#include "../include/isr.h"
#include "../include/io.h"
#include "../include/console.h"
#include "../include/io.h"
#include "../include/isr.h"

void pit_handler(struct registers_t *r){
u8 dummy = inportb(PIT_CH0_DATA_PORT);
u64 ticks;
const u32 freq = 100;

pic_eoi(IRQ_BASE);
}
void pit_handler(struct registers_t *r) { ticks += 1; }

void init_pit(){
printf("Initiating PIT...\n");
isr_register_interrupt_handler(IRQ_TIMER, pit_handler);

//setup PIT into PIC
outportb(PIC_MASTER_COMMAND, PIC_ICW1 | PIC_ICW4_8086);
outportb(PIC_SLAVE_COMMAND, PIC_ICW1 | PIC_ICW4_8086);
outportb(PIC_MASTER_DATA, 1 << IRQ_TIMER);
outportb(PIC_SLAVE_DATA, IRQ_TIMER);
u8 current_mask = inportb(PIC_MASTER_DATA);
current_mask &= ~(1 << IRQ_TIMER); // Clear the bit to enable the PIT interrupt
outportb(PIC_MASTER_DATA, current_mask);
void init_pit() {
printf("Initiating PIT...\n");
isr_register_interrupt_handler(IRQ_BASE + IRQ_TIMER, pit_handler);
start_pit_timer();
}

void start_pit_timer(u16 frequency){
if (frequency == 0) {
printf("Can't divide by zero!");
return;
}
void start_pit_timer() {
if (freq == 0) {
printf("Can't divide by zero!");
return;
}

u16 divisor = 1193180 / freq;

u16 divisor = 1193180 / frequency; // Calculate the divisor for the desired frequency
outportb(PIT_COMMAND_PORT, PIT_COMMAND_REGISTER);
outportb(PIT_CH0_DATA_PORT, (u8)(divisor & 0xFF)); // Send the low byte
outportb(PIT_CH0_DATA_PORT, (u8)((divisor >> 8) & 0xFF)); // Send the high byte
}

// Set the PIT to the desired frequency
outportb(PIT_COMMAND_PORT, PIT_COMMAND_REGISTER);
outportb(PIT_CH0_DATA_PORT, divisor & 0xFF); // Send the low byte
outportb(PIT_CH0_DATA_PORT, divisor >> 8); // Send the high byte
void stop_pit_timer() { outportb(PIT_COMMAND_PORT, 0x30); }

u8 read_pit(u8 channel) {
u8 port;
switch (channel) {
case 0:
port = PIT_CH0_DATA_PORT;
break;
case 1:
port = PIT_CH1_DATA_PORT;
break;
case 2:
port = PIT_CH2_DATA_PORT;
break;
default:
// Handle invalid channel
break;
}

// Read from the specified channel
u8 value = inportb(port);
return value;
}

void stop_pit_timer(){
outportb(PIT_COMMAND_PORT, 0x30);
void write_pit(u8 channel, u8 value) {
u8 port;
switch (channel) {
case 0:
port = PIT_CH0_DATA_PORT;
break;
case 1:
port = PIT_CH1_DATA_PORT;
break;
case 2:
port = PIT_CH2_DATA_PORT;
break;
default:
// Handle invalid channel
break;
}

// Write to the specified channel
outportb(port, value);
}

void sleep(u32 milliseconds){
u32 ticks = milliseconds * 1000 / PIT_TICKS_PER_SECOND; // Calculate the number of ticks
void sleep(u32 milliseconds) {
// Calculate the number of PIT ticks needed for the delay
u32 ticks = milliseconds * 1000 / ticks;

// Save the current PIT counter value
// Save the current PIT channel 0 count
u8 initialCounter = inportb(PIT_CH0_DATA_PORT);

// Start the PIT timer
start_pit_timer((u16)PIT_TICKS_PER_SECOND);
start_pit_timer();

// Wait for the specified number of ticks
while (ticks > 0) {
// Wait for the PIT interrupt

// Decrement the remaining ticks
// Wait for PIT interrupt
ticks--;
}

// Stop the PIT timer
stop_pit_timer();

// Restore the initial PIT counter value
outportb(PIT_COMMAND_PORT, PIT_COMMAND_REGISTER); // Set the PIT back to the initial state
outportb(PIT_CH0_DATA_PORT, initialCounter & 0xFF); // Restore the low byte
outportb(PIT_CH0_DATA_PORT, initialCounter >> 8); // Restore the high byte
}

u8 read_pit(u8 channel){
u8 port;
switch (channel) {
case 0:
port = PIT_CH0_DATA_PORT;
break;
case 1:
port = PIT_CH1_DATA_PORT;
break;
case 2:
port = PIT_CH2_DATA_PORT;
break;
default:
// Handle invalid channel
break;
}

// Read from the specified channel
u8 value = inportb(port);
return value;
}

void write_pit(u8 channel, u8 value){
u8 port;
switch (channel) {
case 0:
port = PIT_CH0_DATA_PORT;
break;
case 1:
port = PIT_CH1_DATA_PORT;
break;
case 2:
port = PIT_CH2_DATA_PORT;
break;
default:
// Handle invalid channel
break;
}

// Write to the specified channel
outportb(port, value);
// Restore the initial PIT channel 0 count
outportb(PIT_COMMAND_REGISTER, 0x36); // Set the operating mode to square wave generator
outportb(PIT_CH0_DATA_PORT, initialCounter); // Set the initial counter value
}
3 changes: 1 addition & 2 deletions src/arch/i386/kernel/include/drivers/cmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#define CMOS_ADDR_REG 0x70
#define CMOS_DATA_REG 0x71

//RTC register addresses
#define RTC_SECONDS 0x00
#define RTC_MINUTES 0x02
#define RTC_HOURS 0x04
#define RTC_DAY 0x07
#define RTC_MONTH 0x08
#define RTC_YEAR 0x09
#define RTC_YEAR 0x09 //last two digits

void init_cmos();
u8 cmos_read(u8 reg);
Expand Down
3 changes: 1 addition & 2 deletions src/arch/i386/kernel/include/drivers/pit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
#define PIT_CH2_DATA_PORT 0x42
#define PIT_COMMAND_PORT 0x43
#define PIT_COMMAND_REGISTER 0x36
#define PIT_TICKS_PER_SECOND 65536

#include "../libc/include/types.h"

void init_pit();
void start_pit_timer(u16 frequency);
void start_pit_timer();
void stop_pit_timer();
void sleep(u32 milliseconds);
u8 read_pit(u8 channel);
Expand Down
2 changes: 1 addition & 1 deletion src/arch/i386/kernel/include/isr.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extern void irq_15();
#define IRQ_DISKETTE_DRIVE 0x06
#define IRQ_PARALLEL_PORT 0x07
#define IRQ_CMOS 0x08
#define IRQ_CGA 0x09
#define IRQ_ACPI 0x09
#define IRQ_NIC1 0x0A
#define IRQ_NIC2 0x0B
#define IRQ_AUXILIARY 0x0C
Expand Down
8 changes: 6 additions & 2 deletions src/arch/i386/kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ void kmain()
init_gdt();
init_pic();
init_idt();
//init_pit();
init_pit();
init_cmos();
init_fpu();
init_keyboard();

while (1)
{
printf(shell);
printf("%s", shell);
memset(buffer, 0, sizeof(buffer));
getstr_bound(buffer, strlen(shell));

Expand Down Expand Up @@ -108,8 +108,12 @@ void kmain()
u8 seconds = cmos_read(RTC_SECONDS);
u8 minutes = cmos_read(RTC_MINUTES);
u8 hours = cmos_read(RTC_HOURS);
u8 day = cmos_read(RTC_DAY);
u8 month = cmos_read(RTC_MONTH);
u8 year = cmos_read(RTC_YEAR);

printf("Time: %d:%d:%d\n", hours, minutes, seconds);
printf("Date: %d/%d/%d\n", day, month, year);
}else
{
printf("invalid command: %s\n", buffer);
Expand Down

0 comments on commit 713bb3d

Please sign in to comment.