Files
kern/include/interrupt/pit.h
T
Tom-on ff07696dc6 Continuing terminal I/O
- Made stdio.h work
- Moved printFiletable back into kernel.c
- TODO: Add stdio.h to all files that should use it (probably next commmit)
- Added some more global addresses. There's a memory layout, but i'm not using it
- Got cursor to be drawn
- Calc program now gets user input
2024-05-28 17:08:49 +02:00

54 lines
1.5 KiB
C

#ifndef TIMER_H
#define TIMER_H
#include <interrupt/idt.h>
#include <interrupt/pic.h>
#include <terminal/terminal.h>
#include <memory/addresses.h>
#include <stdint.h>
#define CHANNEL_0 0x40 // Offset it by 1 or 2 for ch1 and ch2
#define COMMAND 0x43
#define INPUT_CLK 1193182
static uint32_t* sleepTimerTicks = (uint32_t*)SLEEP_TIMER;
__attribute__ ((interrupt))
void timerHandler(intFrame_t* iframe) {
static uint32_t cursorTicks = 0;
cursorTicks++;
if (terminal->showCursor) {
if (cursorTicks % 500 == 0) putcAt(127, terminal->x, terminal->y);
else if (cursorTicks % 250 == 0) putcAt(' ', terminal->x, terminal->y);
}
if (*sleepTimerTicks > 0) { --(*sleepTimerTicks); }
sendPicEOI(0);
}
void setPitPhase(uint8_t channel, uint8_t opMode, uint16_t hz) {
if (channel > 2 || opMode > 7) { return; }
__asm__ volatile ("cli");
// Send command - bits 7-6 channel, 5-4 Access mode, 3-1 operating mode, 0 - Should be zero in x86
outb(COMMAND, ((channel << 6) | (3 << 4) | (opMode << 1)));
// Send frequency
uint16_t freq = INPUT_CLK / hz; // I solved an equation to figure this out
outb(CHANNEL_0 + channel, (uint8_t)freq);
outb(CHANNEL_0 + channel, (uint8_t)(freq >> 8));
__asm__ volatile ("sti");
}
void setupTimer() { // Sets up IRQ 0 - PIT
idtSetGate(0x20, timerHandler, INT_GATE_FLAGS);
unsetIrqMask(0);
setPitPhase(0, 2, 1000); // Default setup: 1000Hz - 1 tick per 1ms
}
#endif