Compare commits
No commits in common. "26dd32345f47e959c2172f5b22efa468a8a16be9" and "4899877cec60dc976ef2aa66a32cb3950c901da9" have entirely different histories.
26dd32345f
...
4899877cec
7 changed files with 26 additions and 179 deletions
|
|
@ -1,16 +0,0 @@
|
|||
/* asm.h
|
||||
* Assembly wrappers
|
||||
*
|
||||
* Wrapper functions for using assembly files.
|
||||
*/
|
||||
#ifndef ASM_H
|
||||
#define ASM_H
|
||||
#include <stdint.h>
|
||||
|
||||
#define COM1 0x3F8
|
||||
#define COM2 0x2F8
|
||||
|
||||
void outb(uint16_t port, uint8_t data);
|
||||
uint8_t inb(uint16_t port);
|
||||
|
||||
#endif
|
||||
|
|
@ -7,20 +7,6 @@
|
|||
#define KTTOOLS_H
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief Convert a hex int to string, up to 32 bits.
|
||||
*
|
||||
* Converts int "num" into a null-terminated string, placed into buffer "buf".
|
||||
* Evaluates from right to left, so left hand digits will be cut off if the buffer is too small
|
||||
* Should work for non 32-bit integers actually, but is uint32_t for now.
|
||||
*
|
||||
* @param num: Value to convert to string
|
||||
* @param buf: Memory to place the string into
|
||||
* @param size: Size of the memory buffer
|
||||
* @param radix: Base counting system to use for output
|
||||
* @return No return value
|
||||
*
|
||||
*/
|
||||
void i_to_str(uint32_t num, char* buf, int size, int radix);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
/* serial.h
|
||||
* Serial Interface
|
||||
*
|
||||
* Send/receive data via serial ports.
|
||||
*/
|
||||
#ifndef SERIAL_H
|
||||
#define SERIAL_H
|
||||
#include "asm.h"
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct SerialState_s {
|
||||
uint16_t port;
|
||||
} SerialState_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize a serial port for communication
|
||||
* @param port The serial port number to initialize
|
||||
* @return Status code indicating success or failure
|
||||
*/
|
||||
int serial_init(uint16_t port);
|
||||
|
||||
/**
|
||||
* @brief Check if there is pending data to receive on the serial port
|
||||
* @return Non-zero if data is pending, 0 otherwise
|
||||
*/
|
||||
int serial_recv_pending();
|
||||
|
||||
/**
|
||||
* @brief Receive a single byte from the serial port
|
||||
* @return The received byte as an 8-bit unsigned integer
|
||||
*/
|
||||
uint8_t serial_recv8(uint16_t port);
|
||||
|
||||
/**
|
||||
* @brief Check if the serial port is ready to send data
|
||||
* @return Non-zero if ready to send, 0 otherwise
|
||||
*/
|
||||
int serial_send_pending(uint16_t port);
|
||||
|
||||
/**
|
||||
* @brief Send a single byte through the serial port
|
||||
* @param data The 8-bit data byte to send
|
||||
*/
|
||||
int serial_send8(uint16_t port, char data);
|
||||
|
||||
|
||||
#endif
|
||||
13
src/asm.s
13
src/asm.s
|
|
@ -1,13 +0,0 @@
|
|||
global outb, inb
|
||||
section .text
|
||||
outb:
|
||||
mov dx, [esp+4]
|
||||
mov al, [esp+8]
|
||||
out dx, al
|
||||
ret
|
||||
|
||||
inb:
|
||||
mov eax, 0
|
||||
mov dx, [esp+4]
|
||||
in al, dx
|
||||
ret
|
||||
|
|
@ -12,6 +12,20 @@
|
|||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Convert a hex int to string, up to 32 bits.
|
||||
*
|
||||
* Converts int "num" into a null-terminated string, placed into buffer "buf".
|
||||
* Evaluates from right to left, so left hand digits will be cut off if the buffer is too small
|
||||
* Should work for non 32-bit integers actually, but is uint32_t for now.
|
||||
*
|
||||
* @param num: Value to convert to string
|
||||
* @param buf: Memory to place the string into
|
||||
* @param size: Size of the memory buffer
|
||||
*
|
||||
* @return No return value
|
||||
*
|
||||
*/
|
||||
void i_to_str(uint32_t num, char* buf, int size, int radix) {
|
||||
//null terminate the string
|
||||
if(num == 0){
|
||||
|
|
|
|||
41
src/main.c
41
src/main.c
|
|
@ -1,50 +1,33 @@
|
|||
//Our own code, at this point...
|
||||
//Output table:
|
||||
//0: VGA
|
||||
//1: COM1
|
||||
#define OUTPUT_TYPE 1
|
||||
//#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "vga.h"
|
||||
#include "io.h"
|
||||
#include "kttools.h"
|
||||
#include "kmultiboot.h"
|
||||
#include "idt.h"
|
||||
#include "serial.h"
|
||||
//finally, main.
|
||||
void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info)
|
||||
{
|
||||
//Hello C! Let's get to work in cleaning up our environment a bit and creating some safety.
|
||||
//First interrupts.
|
||||
setup_idt();
|
||||
|
||||
//Let's pick an output schema.
|
||||
int (*outWriter)(char) = 0;
|
||||
int comstatus = 1234;
|
||||
switch(OUTPUT_TYPE) {
|
||||
case 1:
|
||||
comstatus = serial_init(COM1);
|
||||
if(comstatus) {
|
||||
//Fail...
|
||||
break;
|
||||
}
|
||||
outWriter = serial_send8;
|
||||
break;
|
||||
}
|
||||
//VGA is selected or the selected option failed, falling back
|
||||
if(outWriter == 0){
|
||||
outWriter = vga_out;
|
||||
vga_clear();
|
||||
}
|
||||
|
||||
//wipe the screen
|
||||
vga_clear();
|
||||
//We're going to use this buffer as our 8char hex representation for reading mem
|
||||
|
||||
printf(outWriter, "Entry eax:%X\n", multiboot_magic);
|
||||
printf(vga_out, "Entry eax:%X\n", multiboot_magic);
|
||||
|
||||
if(multiboot_magic != 0x2BADB002) {
|
||||
println(outWriter, writerState, "Bootloader not multiboot1 compliant! Needed for mmap, etc. Can't work without it, kthxbye!");
|
||||
println(vga_out, "Bootloader not multiboot1 compliant! Needed for mmap, etc. Can't work without it, kthxbye!");
|
||||
return;
|
||||
} else {
|
||||
println(outWriter, writerState, "Multiboot detected! Continuing...");
|
||||
println(vga_out, "Multiboot detected! Continuing...");
|
||||
}
|
||||
|
||||
printf(outWriter, writerState, "MEM_LOWER:%X\n", multiboot_info->mem_lower);
|
||||
printf(outWriter, writerState"MEM_UPPER:%X\n", multiboot_info->mem_upper);
|
||||
printf(vga_out, "MEM_LOWER:%X\n", multiboot_info->mem_lower);
|
||||
printf(vga_out, "MEM_UPPER:%X\n", multiboot_info->mem_upper);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
60
src/serial.c
60
src/serial.c
|
|
@ -1,60 +0,0 @@
|
|||
#include "serial.h"
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
int serial_init(uint16_t port) {
|
||||
//disable interrupts when messing with it
|
||||
outb(port + 1, 0x00);
|
||||
//set the dlab latch in order to make the baud rate accessible. In binary for clarity.
|
||||
outb(port + 3, 0b10000000);
|
||||
|
||||
//Sets the baud rate. Quick except on baud rate:
|
||||
//internal clock for the serial controller is 115200 ticks/sec. Or 115200hz.
|
||||
//This value we set, the "clock divisor", will check it every x ticks.
|
||||
//So for example, if we wanted to check it every second, we'd want a divisor of 115200. Or, to check it as fast as possible, 1.
|
||||
//Higher baud rates however equate to more error, so keep the divisor high.
|
||||
//In ourcase, the osdev wiki uses 3 for the divisor, or 38400 baud, and we will go with this example until further notice.
|
||||
//(Source:https://wiki.osdev.org/Serial_Ports#Baud_Rate)
|
||||
|
||||
//Baud lo byte
|
||||
outb(port, 0x03);
|
||||
//Baud hi byte
|
||||
outb(port+1, 0x00);
|
||||
|
||||
//Again from OSWiki we're going to use their defaults for now.
|
||||
//DLAB off
|
||||
//Parity=0(no Parity)
|
||||
//One stop bit (set to 0)
|
||||
//Transmit in units of 8 bits
|
||||
outb(port+3, 0b00000011);
|
||||
|
||||
//FIFO Control register
|
||||
//14 bit threshold for interrupt trigger
|
||||
//flush and enable FIFOs.
|
||||
outb(port+2, 0b11000111);
|
||||
|
||||
//Enable IRQs + DTR + RTS
|
||||
//Nicely explained here: https://stackoverflow.com/questions/957337/what-is-the-difference-between-dtr-dsr-and-rts-cts-flow-control
|
||||
//In the future, this will be referred to 0x0B.
|
||||
outb(port+4, 0b00001011);
|
||||
|
||||
//done! Let's test our chip. put it in loopback mode.
|
||||
outb(port+4, 0x1E);
|
||||
//Lets try sending C4 (boom!) to test it
|
||||
outb(port, 0xC4);
|
||||
|
||||
if(inb(port) != 0xC4) {
|
||||
//Boo womp
|
||||
return 1;
|
||||
}
|
||||
|
||||
//we're good! All set up. Lets put it back into normal operational mode and gtfo.
|
||||
outb(port+4, 0x0B);
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int serial_send_pending(uint16_t port) {
|
||||
//Bit 5 of the line status register has our output queue, essentially
|
||||
return !(inb(port + 5) & 0x20);
|
||||
}
|
||||
Loading…
Reference in a new issue