Skip to content

Commit

Permalink
add timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Moros1138 committed Oct 10, 2021
1 parent 6252f9f commit 49ca6db
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ KERNEL_OBJS :=\
$(OBJ)/isr.o \
$(OBJ)/io.o \
$(OBJ)/monitor.o \
$(OBJ)/timer.o \
$(OBJ)/memory.o \
$(OBJ)/random.o \
$(OBJ)/main.o
Expand Down
26 changes: 26 additions & 0 deletions include/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _MOROS_TIMER_H
#define _MOROS_TIMER_H

#include "monitor.h"
#include "io.h"
#include "isr.h"

namespace MorOS
{
class Timer
{
public:
Timer();
Timer(uint32_t freq);
~Timer();

void init(uint32_t freq);
void set(uint32_t freq);

static void callback(registers_t regs);
static uint32_t tick;
}; // Timer

} // MorOS

#endif // _MOROS_TIMER_H
17 changes: 8 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "monitor.h"
#include "gdt.h"
#include "idt.h"
#include "timer.h"
#include "memory.h"

extern "C" void _main(multiboot_info_t* mbd, uint32_t)
Expand All @@ -12,7 +13,7 @@ extern "C" void _main(multiboot_info_t* mbd, uint32_t)
MorOS::Monitor monitor{};
MorOS::GlobalDescriptorTable gdt{};
MorOS::InterruptManager interruptManager{};

multiboot_memory_map_t* mmmt = 0;

// cycle through the memory map in search of the memory chunk at 1MiB
Expand All @@ -31,12 +32,10 @@ extern "C" void _main(multiboot_info_t* mbd, uint32_t)
// if we have a memory chunk at 1MiB, let's totally use it!
if(mmmt != 0)
MorOS::MemoryManager memoryManager(mmmt->addr_low, mmmt->len_low);

// moment of fucking truth
asm volatile("int $0x04;");
asm volatile("int $0x05;");
asm volatile("int $0x06;");
asm volatile("int $0x07;");

// hang out here forever

MorOS::Timer(100);


// just chill out forever!
while(1);
}
52 changes: 52 additions & 0 deletions src/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "timer.h"

namespace MorOS
{
// define it so we can use it in the callback!
uint32_t Timer::tick = 0;

Timer::Timer()
{
tick = 0;
register_interrupt_handler(IRQ0, &MorOS::Timer::callback);
}

Timer::Timer(uint32_t freq)
{
tick = 0;
register_interrupt_handler(IRQ0, &MorOS::Timer::callback);
set(freq);
}
Timer::~Timer()
{ }

void Timer::set(uint32_t freq)
{
// The value we send to the PIT is the value to divide it's input clock
// PIT input clock: 1193180 Hz

// divide the PIT clock speed by the frequency we want
// divisor must be small enough to fit into 16-bits.
// the higher the divisor the faster the interrupt
uint32_t divisor = 1193180 / freq;


// Send the command byte.
outb(0x43, 0x36);

// Divisor has to be sent byte-wise, so split here into upper/lower bytes.
uint8_t l = (uint8_t)(divisor & 0xFF);
uint8_t h = (uint8_t)( (divisor>>8) & 0xFF );

// Send the frequency divisor.
outb(0x40, l);
outb(0x40, h);
}

void Timer::callback(registers_t regs)
{
tick++;
printf("Pulse: %u\n", tick);
}

} // MorOS

0 comments on commit 49ca6db

Please sign in to comment.