Skip to content

Commit

Permalink
Adding time as separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
artiepoole committed Jul 13, 2024
1 parent 119c1af commit 778ec17
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 66 deletions.
1 change: 1 addition & 0 deletions include/IDT.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "system.h"
#include "PIC.h"
#include "EventQueue.h"
#include "Time.h"



Expand Down
60 changes: 2 additions & 58 deletions include/PIC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ void PIC::enableIRQ(const u8 i)
log.write(byte, true);
log.newLine();
}
else
else if (i<16)
{
const u8 old_mask2 = inb(PIC2_DATA);
const u8 byte = 0x1 << (i - 8);
mask2 = old_mask2 & byte;
log.write(byte, true);
log.newLine();
}
else return;

outb(PIC1_DATA, mask1);
outb(PIC2_DATA, mask2);
Expand All @@ -101,60 +102,3 @@ void PIC::enableAll()
}


volatile u32 ticks = 0;
u32 rate = 0;


// extern "C"
void configurePit(const u32 hz)
{
auto& log = Serial::get();
const u32 divisor = 1193180 / hz; /* Calculate our divisor */
rate = hz;
log.write("Configured PIT. Divisor: ");
log.write(divisor);
log.newLine();
outb(0x43, 0x36); /* Set our command byte 0x36 */
outb(0x40, divisor & 0xFF); /* Set low byte of divisor */
outb(0x40, divisor >> 8); /* Set high byte of divisor */
}

void sleep(const u32 ms)
{
auto& log = Serial::get();
if (rate == 0)
{
log.write("Tried to sleep when timer is not initiated.");
return;
}

ticks = ms * rate / 1000; // rate is in hz, time is in ms

log.write("Sleeping for ");
log.write(ms);
log.write("ms. Ticks: ");
log.write(ticks);
log.write(" Rate: ");
log.write(rate);
log.newLine();
while (ticks > 0);
//log.write("Exited while loop. ");
//log.write("Remaining ticks: ");
//log.write_int(ticks);
// log.new_line();
}

void timerHandler()
{
/* Increment our 'tick count' */
if (ticks == 0) return;

ticks = ticks - 1;

/* Every 18 clocks (approximately 1 second), we will
* display a message on the screen */
// if (ticks % rate == 0)
// {
// log.write("One second has passed\n");
// }
}
4 changes: 0 additions & 4 deletions include/PIC.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class PIC
static void enableAll();
};

void timerHandler();
void sleep(u32 ms);
// extern "C"
void configurePit(u32 hz);


#endif //PIC_H
62 changes: 62 additions & 0 deletions include/Time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Created by artypoole on 13/07/24.
//

#include "Time.h"

volatile u32 timer_ticks = 0;
volatile u64 timer_count = 0;
u32 rate = 0;



// extern "C"
void configurePit(const u32 hz)
{
auto& log = Serial::get();
const u32 divisor = 1193180 / hz; /* Calculate our divisor */
rate = hz;
log.write("Configured PIT. Divisor: ");
log.write(divisor);
log.newLine();
outb(0x43, 0x36); /* Set our command byte 0x36 */
outb(0x40, divisor & 0xFF); /* Set low byte of divisor */
outb(0x40, divisor >> 8); /* Set high byte of divisor */
}

void sleep(const u32 ms)
{
auto& log = Serial::get();
if (rate == 0)
{
log.write("Tried to sleep when timer is not initiated.");
return;
}

timer_ticks = ms * rate / 1000; // rate is in hz, time is in ms

log.write("Sleeping for ");
log.write(ms);
log.write("ms. Ticks: ");
log.write(timer_ticks);
log.write(" Rate: ");
log.write(rate);
log.newLine();
while (timer_ticks > 0);
//log.write("Exited while loop. ");
//log.write("Remaining timer_ticks: ");
//log.write_int(timer_ticks);
// log.new_line();
}

void timerHandler()
{
timer_count = timer_count + 1; // always add one to the time counter

// Check if sleep is still active.
if (timer_ticks == 0) return;

timer_ticks = timer_ticks - 1;


}
18 changes: 18 additions & 0 deletions include/Time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Created by artypoole on 13/07/24.
//

#ifndef TIME_H
#define TIME_H



#include "Serial.h"

void configurePit(u32 hz);

void sleep(u32 ms);

void timerHandler();

#endif //TIME_H
5 changes: 1 addition & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <kernel/kernel.h>


#include "Serial.h"
#include "string.h"
#include "types.h"
#include "multiboot_header.h"
#include "VideoGraphicsArray.h"
#include "IDT.h"
#include "PIC.h"
#include "system.h"
#include "Terminal.h"

/* Check if the compiler thinks you are targeting the wrong operating system. */
Expand Down Expand Up @@ -317,7 +314,7 @@ void kernel_main(const u32 /*stackPointer*/, const multiboot_header* multiboot_s
// sleep(50);
//
// }
// sleep(1000);
// sleep(500);


terminal.setScale(2);
Expand Down

0 comments on commit 778ec17

Please sign in to comment.