1

Setup cmake project

This commit is contained in:
2022-12-07 16:40:43 +01:00
parent db2816a092
commit f304e7f239
138 changed files with 28939 additions and 0 deletions

324
src/bootdisk/bootsect.asm Normal file
View File

@ -0,0 +1,324 @@
; $Id: bootsect.asm 5001 2012-10-12 11:34:05Z os $
;******************************************************************************
;* Betriebssysteme *
;*----------------------------------------------------------------------------*
;* *
;* B O O T S E C T *
;* *
;*----------------------------------------------------------------------------*
;* Code fuer den Disketten-Bootblock des System-Images. Das BIOS laedt den *
;* ersten Block einer Diskette (den Bootblock) beim Starten des Rechner in *
;* den Hauptspeicher und fuehrt ihn aus. Der Programmcode des Bootblocks *
;* laedt nun das restliche System und fuehrt es aus. *
;******************************************************************************
;
; Konstanten
;
BIOSSEG equ 0x07c0 ; Hierher wird der Bootsector
; vom BIOS geladen
BOOTSEG equ 0x0060 ; Hierher verschiebt sich der
; Boot-Code
SETUPSEG equ 0x9000 ; Hierher laedt der Boot-Code den
; Setup-Code (max. 64K inkl. Stack)
SYSTEMSEG equ 0x1000 ; System-Code (max. 512K)
SECTORSZ equ 512 ; Groesse eines Sektors in Bytes
[SECTION .text]
;
; Boot-Code
;
bootsector:
jmp skip_data
;------------------------------------------------------------------------------
;
; Datenbereich, der von 'build' beim Erzeugen der Boot-Diskette
; gefuellt wird.
;
pad:
times 4+bootsector-$ db 0 ; Bytes zum Auffuellen, damit 'total_tracks' an einer
; geraden und tools/build.c bekannten Adresse liegt
total_tracks:
dw 0 ; Anzahl der Tracks der Diskette
total_heads:
dw 0 ; Anzahl der Seiten der Diskette
total_sectors:
dw 0 ; Anzahl der Sektoren pro Track
setup_sectors:
dw 0 ; Anzahl der Sektoren, die der Setup-Code einnimmt
system_sectors:
dw 0 ; Anzahl der Sektoren, die das System einnimmt
bootdevice:
db 0 ; BIOS Geraetecode: 00: Disk A, 01: Disk B, ..., 0x80 HD0, ...
curr_track:
db 0 ; Track, bei dem die Diskette/Partition beginnt
curr_head:
db 0 ; Head, bei dem die Diskette/Partition beginnt
curr_sector:
db 0 ; Sector, bei dem die Diskette/Partition beginnt
;-----------------------------------------------------------------------
;
; Kopieren des Bootsectors
;
skip_data:
mov bl,dl ; vom BIOS uebergebenes Boot-Device sichern
mov ax,BIOSSEG
mov ds,ax
xor si,si
mov ax,BOOTSEG
mov es,ax
xor di,di
mov cx,SECTORSZ/2
rep movsw
;
; Ausfuehrung durch die Kopie fortsetzen
;
jmp BOOTSEG:start
;
; Segmentregister initialisieren und Platz fuer den Stack schaffen
;
start:
mov ax,cs ; Daten-, Stack- und Codesegment sollen
mov ds,ax ; hierher zeigen.
mov ss,ax
mov sp,4*SECTORSZ ; Drei Sektoren als Stack freilassen
mov [bootdevice],bl ; zuvor gesichertes Boot-Device permanent ablegen
;
; Ausgabe einer Meldung mit Hilfe eines BIOS-Aufrufs
;
mov ah,0x03 ; Feststellen der Cursor-Position
xor bh,bh
int 0x10
mov cx,13
mov bx,0x0007 ; page 0, attribute 7 (normal)
mov ax,ds
mov es,ax
mov bp,bootmessage
mov ax,0x1301 ; Ausgabe des Textes, Cursor bewegen
int 0x10
;
; Nachladen des Setup-Codes und des Systems selbst.
;
xor ah,ah ; Reset des Disketten-/Plattencontrollers
mov dl,[bootdevice]
int 0x13
;
; Informationen ueber die Laufwerksgeometrie holen
;
hdd_probe:
mov dl,[bootdevice]
test dl,0x80
jz load_setup ; Floppy mit den Standardparametern laden
mov ah,0x8
int 0x13
jc load_setup ; CF bei Fehler gesetzt
mov [total_heads],dh
mov ax,cx ; CX sichern
and ax,0x3f
mov [total_sectors],ax
mov ax,cx
shr ax,6
mov [total_tracks],ax
;
; Weiterstellen der Disketten-/Plattenposition um 1 (Bootblock)
;
load_setup:
call step_disk
;
; Laden des Setup-Codes
;
mov word [curr_segment],SETUPSEG
mov word [curr_offset],0
mov ax,[setup_sectors]
call load
;
; Laden des Kernels
;
mov word [curr_segment],SYSTEMSEG
mov word [curr_offset],0
mov ax,[system_sectors]
call load
;
; Floppy wieder abschalten
;
call stop_floppy_motor
;
; Start des Setup-Codes
;
mov ax, [system_sectors] ; Speichere Anzahl an System-Sektoren in AX.
jmp SETUPSEG:0
;
; load
;
; Die 'ax' Sektoren von der Diskette in den Hauptspeicher. Die Position auf
; der Diskette muss vorher in curr_head/curr_track/curr_sector und die
; Position im Hauptspeicher in curr_segment/curr_offset stehen. Die Positionen
; werden entsprechend der geladenen Sektoren weitergestellt.
;
load:
mov [to_load],ax
l_next_part:
mov al,[curr_head]
mov [last_head],al
mov al,[curr_track]
mov [last_track],al
mov al,[curr_sector]
mov [last_sector],al
mov ax,[curr_segment]
mov [last_segment],ax
mov ax,[curr_offset]
mov [last_offset],ax
mov al,0
l_loop: call step
cmp byte [curr_sector],0x01
je l_now
cmp word [curr_offset],0x0000
je l_now
cmp al,[to_load]
jne l_loop
l_now:
push ax
mov dl,[bootdevice]
mov dh,[last_head]
mov ch,[last_track]
mov cl,[last_sector]
mov bx,[last_segment]
mov es,bx
mov bx,[last_offset]
mov ah,0x02 ; Funktionscode fuer 'Lesen'
int 0x13
pop ax
push ax
call print_dot
pop ax
mov ah,0
sub [to_load],ax
jne l_next_part
ret
;
; step
;
; Stellt die aktuelle Position im Hauptspeicher und auf der Diskette
; um einen Sektor (512 Byte) weiter.
;
step: add al,1
call step_disk
call step_memory
ret
step_disk:
mov bl,[curr_sector]
add bl,1
mov [curr_sector],bl
cmp bl,[total_sectors]
jle l_1
mov byte [curr_sector],1
mov bl,[curr_head]
add bl,1
mov [curr_head],bl
cmp bl,[total_heads]
jne l_1
mov byte [curr_head],0
mov bl,[curr_track]
add bl,1
mov [curr_track],bl
l_1: ret
step_memory:
mov bx,[curr_offset]
add bx,SECTORSZ
mov [curr_offset],bx
test bx,0xffff
jne l_2
mov bx,[curr_segment]
add bx,0x1000 ; 64 KByte weiterstellen
mov [curr_segment],bx
l_2 ret
;
; Ausgabe eines Stern ('*') mit Hilfe eines BIOS-Aufrufs
;
print_dot:
mov ah,0x03 ; Feststellen der Cursor-Position
xor bh,bh
int 0x10
mov cx,1
mov bx,0x0007 ; page 0, attribute 7 (normal)
mov ax,ds
mov es,ax
mov bp,dot
mov ax,0x1301 ; Ausgabe des Textes, Cursor bewegen
int 0x10
ret
;
; stop_floppy_motor
;
; Stopt den Motor der Floppy, da das BIOS dazu in Kuerze nicht mehr in
; der Lage sein wird. Egal, ob von Floppy oder Platte gebootet wurde.
;
stop_floppy_motor:
mov dx,0x3f2
xor al,al
out dx,al
ret
;
; Datenbereich
;
bootmessage:
db 13,10
db 'booting ... '
dot:
db '*'
to_load:
dw 0
curr_segment:
dw 0
curr_offset:
dw 0
last_head:
db 0
last_track:
db 0
last_sector:
db 0
last_segment:
dw 0
last_offset:
dw 0
unused:
times bootsector+510-$ db 0
mark:
dw 0xaa55

149
src/bootdisk/build.c Executable file
View File

@ -0,0 +1,149 @@
#include <stdio.h> /* fprintf */
#include <string.h>
#include <stdlib.h> /* contains exit */
#include <unistd.h> /* contains read/write */
#include <fcntl.h>
#include <sys/stat.h>
#if !defined(DOS) && !defined(Win32)
#define O_BINARY 0
#endif
#define SECTOR 512
void die(const char *str) {
fprintf(stderr, "%s\n", str);
exit(1);
}
int main(int argc, char **argv) {
int fd, fd_out;
char bootsector[SECTOR];
char setupsector[SECTOR];
struct stat info;
unsigned short setup_size;
int bytes_read, to_read;
unsigned int dc, st, sh, ss, tt, th, ts;
unsigned char bios_device_code, start_track, start_head, start_sector;
unsigned short total_tracks, total_heads, total_sectors;
unsigned short system_sectors;
if (argc != 6)
die("usage: build bootsector setup-code system-image dev.info bootdisk-image\n\n"
"dev.info: BIOS-dev.code:total-tracks:-heads:-sectors:start-track:-head:-sector\n"
"BIOS-devicecode: 0=fd0, 1=fd1, ..., 128=hd0, 129=hd1, ...\n"
"Example: to boot from a 3.5\" floppy disk use \"0:80:2:18:0:0:1\"\n");
sscanf(argv[4], "%u:%u:%u:%u:%u:%u:%u", &dc, &tt, &th, &ts, &st, &sh, &ss);
bios_device_code = (unsigned char) dc;
total_tracks = (unsigned short) tt;
total_sectors = (unsigned short) ts;
total_heads = (unsigned short) th;
start_track = (unsigned char) st;
start_head = (unsigned char) sh;
start_sector = (unsigned char) ss;
printf("BIOS-devicecode: 0x%x\n", bios_device_code);
printf("Total T/H/S: (%d/%d/%d)\n", total_tracks, total_heads,
total_sectors);
printf("Start T/H/S: (%d/%d/%d)\n\n", start_track, start_head,
start_sector);
if ((fd_out = open(argv[5], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644)) < 0)
die("Unable to open output file.");
if ((fd = open(argv[1], O_RDONLY | O_BINARY, 0)) < 0)
die("Unable to open input file.");
if (read(fd, bootsector, SECTOR) < 0)
die("Unable to read bootsector.");
close(fd);
if (stat(argv[2], &info) != 0)
die("Can't stat setup file.");
setup_size = (unsigned short)
((info.st_size + SECTOR - 1) / SECTOR);
printf("Setup size is %d sectors.\n", (int) setup_size);
if (stat(argv[3], &info) != 0)
die("Can't stat system file.");
to_read = info.st_size;
printf("System size is %d bytes.\n", to_read);
system_sectors = (to_read + SECTOR - 1) / SECTOR;
printf("# System sectors is %d.\n", system_sectors);
#if 0
*(unsigned short*)(bootsector+4) = total_tracks;
*(unsigned short*)(bootsector+6) = total_heads;
*(unsigned short*)(bootsector+8) = total_sectors;
*(unsigned short*)(bootsector+10) = setup_size;
*(unsigned short*)(bootsector+12) =
(unsigned short)((to_read + SECTOR - 1) / SECTOR);
*(unsigned char*)(bootsector+14) = bios_device_code;
*(unsigned char*)(bootsector+15) = start_track;
*(unsigned char*)(bootsector+16) = start_head;
*(unsigned char*)(bootsector+17) = start_sector;
#else
*(unsigned char *) (bootsector + 4) = (total_tracks) & 0xff;
*(unsigned char *) (bootsector + 5) = (total_tracks >> 8) & 0xff;
*(unsigned char *) (bootsector + 6) = (total_heads) & 0xff;
*(unsigned char *) (bootsector + 7) = (total_heads >> 8) & 0xff;
*(unsigned char *) (bootsector + 8) = (total_sectors) & 0xff;
*(unsigned char *) (bootsector + 9) = (total_sectors >> 8) & 0xff;
*(unsigned char *) (bootsector + 10) = (setup_size) & 0xff;
*(unsigned char *) (bootsector + 11) = (setup_size >> 8) & 0xff;
*(unsigned char *) (bootsector + 12) = system_sectors & 0xff;
*(unsigned char *) (bootsector + 13) = (system_sectors >> 8) & 0xff;
*(unsigned char *) (bootsector + 14) = bios_device_code;
*(unsigned char *) (bootsector + 15) = start_track;
*(unsigned char *) (bootsector + 16) = start_head;
*(unsigned char *) (bootsector + 17) = start_sector;
#endif
write(fd_out, bootsector, SECTOR);
if ((fd = open(argv[2], O_RDONLY | O_BINARY, 0)) < 0)
die("Unable to open setup file.");
do {
if ((bytes_read = read(fd, setupsector, SECTOR)) < 0)
die("Unable to read setup.");
if (bytes_read > 0)
write(fd_out, setupsector, SECTOR);
} while (bytes_read > 0);
close(fd);
if ((fd = open(argv[3], O_RDONLY | O_BINARY, 0)) < 0)
die("Unable to open system file.");
while (to_read > 0) {
int l;
l = (to_read < SECTOR) ? to_read : SECTOR;
if ((bytes_read = read(fd, setupsector, l)) != l)
die("Unable to read system.");
write(fd_out, setupsector, l);
to_read -= l;
}
close(fd);
close(fd_out);
return 0;
}

200
src/bootdisk/setup.asm Normal file
View File

@ -0,0 +1,200 @@
; $Id: setup.asm 1484 2009-02-11 21:03:19Z hsc $
;******************************************************************************
;* Betriebssysteme *
;*----------------------------------------------------------------------------*
;* *
;* S E T U P *
;* *
;*----------------------------------------------------------------------------*
;* Der Setup-Code liegt im System-Image direkt hinter dem Bootsektor und wird *
;* von diesem direkt nach dem Laden aktiviert. Der Code wird noch im *
;* 'Real-Mode' gestartet, so dass zu Beginn auch noch BIOS-Aufrufe erlaubt *
;* sind. Dann werden jedoch alle Interrupts verboten, die Adressleitung A20 *
;* aktiviert und die Umschaltung in den 'Protected-Mode' vorgenommen. Alles *
;* weitere uebernimmt der Startup-Code des Systems. *
;******************************************************************************
;
; Konstanten
;
SETUPSEG equ 0x9000 ; Setup-Code (max. 64K inkl. Stack)
SYSTEMSEG equ 0x1000 ; System-Code (max. 512K)
SECTORSZ equ 512 ; Groesse eines Sektors in Bytes
SYSTEMSTART equ 0x100000 ; Hierhin wird das System nach Umschalten in den
; Protected Mode kopiert, da GRUB das auch tut
; (und GRUB kann nur an Adressen >1M laden).
[SECTION .text]
[BITS 16]
;
; Segmentregister initialisieren
;
start:
mov dx, ax ; Anzahl Systemsektoren in DX sichern.
mov ax,cs ; Daten-, Code- und Stacksegment sollen
mov ds,ax ; hierher zeigen.
mov ss,ax ; Alle drei Segment duerfen nicht mehr
mov sp,0xfffe ; als 64 KByte einnehmen (zusammen).
mov [system_sectors], dx ; Anzahl der Systemsektoren im Speicher ablegen
;
; Ausgabe einer Meldung mit Hilfe eines BIOS-Aufrufs
;
mov ah,0x03 ; Feststellen der Cursor-Position
xor bh,bh
int 0x10
mov cx,14
mov bx,0x0007 ; page 0, attribute 7 (normal)
mov ax,ds
mov es,ax
mov bp,setupmessage
mov ax,0x1301 ; Ausgabe des Textes, Cursor bewegen
int 0x10
;
; So, jetzt werden die Interrupts abgeschaltet
;
cli ; Maskierbare Interrupts verbieten
mov al,0x80 ; NMI verbieten
out 0x70,al
;
; IDT und GDT setzen
;
lidt [idt_48]
lgdt [gdt_48]
;
; Aktivieren der Adressleitung A20
;
call empty_8042
mov al,0xd1
out 0x64,al
call empty_8042
mov al,0xdf
out 0x60,al
call empty_8042
mov al,0xff
out 0x64,al
call empty_8042
;
; Moeglichen Koprozessor zuruecksetzen
;
xor ax,ax
out 0xf0,al
call delay
out 0xf1,al
call delay
;
; Umschalten in den Protected Mode
;
mov eax,cr0 ; Setze PM-Bit im Kontrollregister 1
or eax,1
mov cr0,eax
jmp dword 0x08:SETUPSEG*0x10+copy_system ; Far-Jump, um
; a) fetch Pipeline zu leeren
; b) CS Register sinnvoll zu belegen
[BITS 32]
; Arbeite jetzt im Protected Mode
copy_system:
;
; Systemcode von 0x10000 nach 0x100000 kopieren.
;
mov ax, 0x10 ; 0x10 entspricht dem Data-Eintrag in der GDT.
mov ds, ax ; DS und ES werden von movsd benoetigt.
mov es, ax
xor ecx, ecx ; Anzahl Systemsektoren laden
mov cx, [SETUPSEG*0x10+system_sectors]
imul ecx, SECTORSZ/4
mov esi, SYSTEMSEG*0x10 ; Hier liegt der Systemcode noch ...
mov edi, SYSTEMSTART ; ... und hierhin moechten wir ihn verschieben
cld ; Nach jedem movsb ESI,EDI inkrementieren
rep movsd ; Kopiere 4 Byte von [ESI] nach [EDI] ecx male
;
; Sprung in den Startup-Code des Systems
;
jmp dword 0x08:SYSTEMSTART
error:
hlt
[BITS 16]
; Ab hier wieder Real-Mode Code fuer die Codeteile
; vor der Umschaltung in den Protected Mode
;
; empty_8042
;
; Ein- und Ausgabepuffer des Tastaturcontrollers leeren
;
empty_8042:
call delay
in al,0x64 ; 8042 Status Port
test al,1 ; Ausgabepuffer voll?
jz no_output
call delay
in al,0x60 ; wenn ja: ueberlesen
jmp empty_8042
no_output:
test al,2 ; Eingabepuffer voll?
jnz empty_8042 ; wenn ja, noch mal testen, irgendwann
ret ; muss es weg sein.
;
; delay:
;
; Kurze Verzoegerung fuer in/out-Befehle
;
delay:
out 0x80,al
ret
;
; Datenbereich
;
[SECTION .data]
; Meldung
system_sectors:
dw 0
setupmessage:
db 13,10
db 'setup active'
;
; Descriptor-Tabellen
;
gdt:
dw 0,0,0,0 ; NULL Deskriptor
dw 0xFFFF ; 4Gb - (0x100000*0x1000 = 4Gb)
dw 0x0000 ; base address=0
dw 0x9A00 ; code read/exec
dw 0x00CF ; granularity=4096, 386 (+5th nibble of limit)
dw 0xFFFF ; 4Gb - (0x100000*0x1000 = 4Gb)
dw 0x0000 ; base address=0
dw 0x9200 ; data read/write
dw 0x00CF ; granularity=4096, 386 (+5th nibble of limit)
dw 0xFFFF ; 4Gb - (0x100000*0x1000 = 4Gb)
dw 0x4000 ; 0x4000 -> base address=0x24000 (siehe BIOS.cc)
dw 09A02h ; 0x2 -> base address =0x24000 (siehe BIOS.cc) und code read/exec;
dw 0008Fh ; granularity=4096, 16-bit code
idt_48:
dw 0 ; idt limit=0
dw 0,0 ; idt base=0L
gdt_48:
dw 0x20 ; GDT Limit=24, 3 GDT Eintraege
dd SETUPSEG*0x10+gdt; Physikalische Adresse der GDT

324
src/device/bios/BIOS.cc Normal file
View File

@ -0,0 +1,324 @@
/*****************************************************************************
* *
* B I O S *
* *
*---------------------------------------------------------------------------*
* Beschreibung: BIOS-Schnittstelle *
* *
* Autor: Michael Schoettner, 29.11.2018 *
*****************************************************************************/
#include "BIOS.h"
#include "kernel/system/Globals.h"
// 16-Bit Code aufrufen, siehe Konstruktor und Aufruf in startup.asm
extern "C" {
void bios_call();
}
// in startup.asm im GDT-Eintrag so festgeschrieben!
constexpr const unsigned int BIOS16_CODE_MEMORY_START = 0x24000;
// Parameter fuer BIOS-Aufrufe (Register)
constexpr const unsigned int BIOS16_PARAM_BASE = 0x26000;
// Zeiger auf Speichbereich fuer Parameter fuer BIOS-Aufruf (siehe BIOS.h)
BIOScall_params* BC_params = reinterpret_cast<BIOScall_params*>(BIOS16_PARAM_BASE);
/*****************************************************************************
* Methode: BIOS::BIOS *
*---------------------------------------------------------------------------*
* Beschreibung: Konstruktor. Baut manuell ein 16-Bit Code Segment fuer *
* den BIOS-Aufruf. Startadresse dieser Funktion steht *
* im 4. GDT-Eintrag (siehe startup.asm). *
*****************************************************************************/
BIOS::BIOS() {
unsigned char* codeAddr = reinterpret_cast<unsigned char*>(BIOS16_CODE_MEMORY_START);
// mov eax, 25000 (Adresse wohin aktuelles esp gesichert wird)
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0xB8;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x50;
codeAddr++;
*codeAddr = 0x02;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
// mov [eax], esp (esp abspeichern)
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0x67;
codeAddr++;
*codeAddr = 0x89;
codeAddr++;
*codeAddr = 0x20;
codeAddr++;
// mov eax,cr0 (cr0 auslesen)
*codeAddr = 0x0F;
codeAddr++;
*codeAddr = 0x20;
codeAddr++;
*codeAddr = 0xC0;
codeAddr++;
// and eax, 7FFEFFFE (Bitmaske zum Abschlaten des Protected-Mode)
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0x25;
codeAddr++;
*codeAddr = 0xFE;
codeAddr++;
*codeAddr = 0xFF;
codeAddr++;
*codeAddr = 0xFE;
codeAddr++;
*codeAddr = 0x7F;
codeAddr++;
// mov cr0, eax (cr0 syetzen um den Protected-Mode auszuschalten)
*codeAddr = 0x0F;
codeAddr++;
*codeAddr = 0x22;
codeAddr++;
*codeAddr = 0xC0;
codeAddr++;
// jmp 2400:001B Instruktions-Pipeline leeren und Dekodierungseinheit auf 16-Bit code umschalten
// Wir springen hier zur naechsten Instruktion (*)
// 2400:001B (2400<<4 = 24000 + 1B)
*codeAddr = 0xEA;
codeAddr++;
*codeAddr = 0x1B;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x24;
codeAddr++;
// (*) mov dx,2400 (Lade 0x2400 nach dx (fuer Parameter-Zugriff -> BIOS16_PARAM_BAS)
*codeAddr = 0xBA;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x24;
codeAddr++;
// mov ss,dx (Lade Stack-Segment-Register)
*codeAddr = 0x8E;
codeAddr++;
*codeAddr = 0xD2;
codeAddr++;
// mov gs,dx
*codeAddr = 0x8E;
codeAddr++;
*codeAddr = 0xEA;
codeAddr++;
// mov esp,2000 -> BIOS16_PARAM_BASE 0x260000 (= 0x2400:2000)
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0xBC;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x20;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
// Register laden (stehen in BIOS16_PARAM_BASE, ab 0x260000)
// (pop erhöht die Adressen)
// pop ds
*codeAddr = 0x1F;
codeAddr++;
// pop es
*codeAddr = 0x07;
codeAddr++;
// pop fs
*codeAddr = 0x0f;
codeAddr++;
*codeAddr = 0xa1;
codeAddr++;
// pop ax
*codeAddr = 0x58;
codeAddr++;
// popad
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0x61;
codeAddr++;
// int(nr)
*codeAddr = 0xCD;
codeAddr++; // 'int' Instruktion
*codeAddr = 0x00;
codeAddr++; // Nummer (wird direkt von BIOS::Int direkt hier reingeschrieben)
// Register speichern in BIOS16_PARAM_BASE (ab 0x260000)
// pushad
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0x60;
codeAddr++;
// pushf
*codeAddr = 0x9C;
codeAddr++;
// push fs
*codeAddr = 0x0f;
codeAddr++;
*codeAddr = 0xa0;
codeAddr++;
// push es
*codeAddr = 0x06;
codeAddr++;
// push ds
*codeAddr = 0x1E;
codeAddr++;
// mov eax,cr0
*codeAddr = 0x0F;
codeAddr++;
*codeAddr = 0x20;
codeAddr++;
*codeAddr = 0xC0;
codeAddr++;
// or eax, 00010001 (protected mode without paging)
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0x0D;
codeAddr++;
*codeAddr = 0x01;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x01;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
// mov cr0, eax
*codeAddr = 0x0F;
codeAddr++;
*codeAddr = 0x22;
codeAddr++;
*codeAddr = 0xC0;
codeAddr++;
// jmp 0018:0049, flush pipeline & switch decoding (active 32 Bit PM)
// 0018:0049
*codeAddr = 0xEA;
codeAddr++;
*codeAddr = 0x49;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x18;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
// mov dx,0010
*codeAddr = 0xBA;
codeAddr++;
*codeAddr = 0x10;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
// mov ds,dx
*codeAddr = 0x8E;
codeAddr++;
*codeAddr = 0xDA;
codeAddr++;
// mov es,dx
*codeAddr = 0x8E;
codeAddr++;
*codeAddr = 0xC2;
codeAddr++;
// mov es,dx
*codeAddr = 0x8E;
codeAddr++;
*codeAddr = 0xE2;
codeAddr++;
// mov fs,dx
*codeAddr = 0x8E;
codeAddr++;
*codeAddr = 0xEA;
codeAddr++;
// mov ss,dx
*codeAddr = 0x8E;
codeAddr++;
*codeAddr = 0xD2;
codeAddr++;
// mov eax, 25000
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0xB8;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
*codeAddr = 0x50;
codeAddr++;
*codeAddr = 0x02;
codeAddr++;
*codeAddr = 0x00;
codeAddr++;
// mov esp, [eax]
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0x67;
codeAddr++;
*codeAddr = 0x8B;
codeAddr++;
*codeAddr = 0x20;
codeAddr++;
// far ret
*codeAddr = 0x66;
codeAddr++;
*codeAddr = 0xCB;
}
/*****************************************************************************
* Methode: BIOS::Int *
*---------------------------------------------------------------------------*
* Beschreibung: Fuehrt einen BIOS-Aufruf per Software-Interrupt durch. *
*****************************************************************************/
void BIOS::Int(int inter) {
unsigned char* ptr = reinterpret_cast<unsigned char*>(BIOS16_CODE_MEMORY_START);
// Interrupt-Nummer in 16-Bit Code-Segment schreiben (unschoen, aber ...)
*(ptr + 48) = static_cast<unsigned char>(inter);
CPU::disable_int(); // Interrupts abschalten
bios_call();
CPU::enable_int();
}

53
src/device/bios/BIOS.h Normal file
View File

@ -0,0 +1,53 @@
/*****************************************************************************
* *
* B I O S *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Zugriff auf das 16-Bit BIOS. Fuer VESA-Funktionen. *
* *
* Autor: Michael Schoettner, 13.9.2016 *
*****************************************************************************/
#ifndef BIOS_include__
#define BIOS_include__
// Speicherseite fuer Rueckgabewerte von BIOS-Aufrufen
constexpr const unsigned int RETURN_MEM = 0x9F000;
// Struktur fuer Parameteruebergabe fuer einen BIOS-Aufruf
struct BIOScall_params {
unsigned short DS;
unsigned short ES;
unsigned short FS;
unsigned short Flags;
unsigned int DI;
unsigned int SI;
unsigned int BP;
unsigned int SP;
unsigned int BX;
unsigned int DX;
unsigned int CX;
unsigned int AX;
} __attribute__((packed));
// kein Auffuellen von bytes auf Wortgrenzen
// Zeiger auf Speichbereich fuer Parameter fuer BIOS-Aufruf
extern BIOScall_params* BC_params;
class BIOS {
private:
// Initialisierung: manuelles Anlegen einer Funktion
BIOS();
public:
BIOS(const BIOS& copy) = delete; // Verhindere Kopieren
static BIOS& instance() {
static BIOS bios;
return bios;
}
// BIOS-Aufruf, per Software-Interrupt
static void Int(int inter);
};
#endif

52
src/device/cpu/CPU.h Executable file
View File

@ -0,0 +1,52 @@
/*****************************************************************************
* *
* C P U *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung einer Abstraktion fuer den Prozessor. *
* Derzeit wird nur angeboten, Interrupts zuzulassen, zu *
* verbieten oder den Prozessor anzuhalten. *
* *
* Autor: Michael Schoettner, 30.7.16 *
*****************************************************************************/
#ifndef CPU_include__
#define CPU_include__
class CPU {
public:
CPU(const CPU& copy) = delete; // Verhindere Kopieren
CPU() = default;
// Erlauben von (Hardware-)Interrupts
static inline void enable_int() {
asm volatile("sti");
}
// Interrupts werden ignoriert/verboten
static inline void disable_int() {
asm volatile("cli");
}
// Prozessor bis zum naechsten Interrupt anhalten
static inline void idle() {
asm volatile("sti;"
"hlt");
}
// Prozessor anhalten
static inline void halt() {
asm volatile("cli;"
"hlt");
}
// Time-Stamp-Counter auslesen
static inline unsigned long long int rdtsc() {
unsigned long long int ret;
asm volatile("rdtsc"
: "=A"(ret));
return ret;
}
};
#endif

201
src/device/graphics/CGA.cc Executable file
View File

@ -0,0 +1,201 @@
/*****************************************************************************
* *
* C G A *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Mit Hilfe dieser Klasse kann man auf den Bildschirm des *
* PCs zugreifen. Der Zugriff erfolgt direkt auf der Hard- *
* wareebene, d.h. ueber den Bildschirmspeicher und den *
* I/O-Ports der Grafikkarte. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
* Aenderungen von Michael Schoettner, HHU, 21.8.2016 *
*****************************************************************************/
#include "CGA.h"
#include "lib/mem/Memory.h"
const IOport CGA::index_port(0x3d4);
const IOport CGA::data_port(0x3d5);
const bse::span<CGA::cga_char_t, CGA::ROWS * CGA::COLUMNS> CGA::SCREEN{reinterpret_cast<CGA::cga_char_t*>(0xb8000U)};
const bse::span<CGA::cga_line_t, CGA::ROWS> CGA::SCREEN_ROWS{reinterpret_cast<CGA::cga_line_t*>(0xb8000U)};
CGA::cga_page_t* const CGA::SCREEN_PAGE {reinterpret_cast<CGA::cga_page_t*>(0xb8000U)};
/*****************************************************************************
* Methode: CGA::setpos *
*---------------------------------------------------------------------------*
* Beschreibung: Setzen des Cursors in Spalte x und Zeile y. *
*****************************************************************************/
void CGA::setpos(unsigned int x, unsigned int y) {
/* Hier muess Code eingefuegt werden */
// NOTE: The cursor addresses positions on screen, not bytes
unsigned short pos = x + y * COLUMNS;
unsigned char cursor_low = pos & 0xFF;
unsigned char cursor_high = (pos >> 8) & 0xFF;
index_port.outb(0xF); // Cursor(low)
data_port.outb(cursor_low);
index_port.outb(0xE); // Cursor(high)
data_port.outb(cursor_high);
}
/*****************************************************************************
* Methode: CGA::getpos *
*---------------------------------------------------------------------------*
* Beschreibung: Abfragem der Cursorposition *
* *
* Rückgabewerte: x und y *
*****************************************************************************/
void CGA::getpos(unsigned int& x, unsigned int& y) {
/* Hier muess Code eingefuegt werden */
index_port.outb(0xF); // Cursor(low)
unsigned char cursor_low = data_port.inb();
index_port.outb(0xE); // Cursor(high)
unsigned char cursor_high = data_port.inb();
unsigned short cursor =
(cursor_low & 0xFF) | ((cursor_high << 8) & 0xFF00);
x = cursor % COLUMNS;
y = (cursor / COLUMNS);
}
/*****************************************************************************
* Methode: CGA::show *
*---------------------------------------------------------------------------*
* Beschreibung: Anzeige eines Zeichens mit Attribut an einer bestimmten *
* Stelle auf dem Bildschirm. *
* *
* Parameter: *
* x,y Position des Zeichens *
* character Das auszugebende Zeichen *
* attrib Attributbyte fuer das Zeichen *
*****************************************************************************/
void CGA::show(unsigned int x, unsigned int y, char character, unsigned char attrib) {
/* Hier muess Code eingefuegt werden */
if (x >= COLUMNS || y >= ROWS) {
// Out of bounds
return;
}
cga_char_t* pos = SCREEN[x + y * COLUMNS];
pos->cga_char = character;
pos->cga_attribute = attrib;
}
/*****************************************************************************
* Methode: CGA::print *
*---------------------------------------------------------------------------*
* Beschreibung: Anzeige mehrerer Zeichen ab der aktuellen Cursorposition *
* '\n' fuer Zeilenvorschub. *
* *
* Parameter: *
* substring Auszugebende Zeichenkette *
* n Laenger der Zeichenkette *
* attrib Attributbyte fuer alle Zeichen der Zeichenkette *
*****************************************************************************/
void CGA::print(const bse::string_view string, unsigned char attrib) const {
/* Hier muess Code eingefuegt werden */
unsigned int cursor_x = 0;
unsigned int cursor_y = 0; // Don't poll registers every stroke
getpos(cursor_x, cursor_y);
for (char current : string) {
if (current == '\n') {
cursor_x = 0;
cursor_y = cursor_y + 1;
if (cursor_y >= ROWS) {
// Bottom of screen reached
scrollup();
cursor_y = cursor_y - 1;
}
continue;
}
if (current == '\0') {
// Don't need to run to end if null terminated
break;
}
show(cursor_x, cursor_y, current, attrib);
cursor_x = cursor_x + 1;
if (cursor_x >= COLUMNS) {
// Right of screen reached
cursor_x = 0;
cursor_y = cursor_y + 1;
if (cursor_y >= ROWS) {
// Bottom of screen reached
scrollup();
cursor_y = cursor_y - 1;
}
}
}
setpos(cursor_x, cursor_y);
}
/*****************************************************************************
* Methode: CGA::scrollup *
*---------------------------------------------------------------------------*
* Beschreibung: Verschiebt den Bildschirminhalt um eine Zeile nach oben. *
* Die neue Zeile am unteren Bildrand wird mit Leerzeichen *
* gefuellt. *
*****************************************************************************/
void CGA::scrollup() const {
/* Hier muss Code eingefuegt werden */
// Move up
bse::memcpy<cga_line_t>(SCREEN_ROWS[0], SCREEN_ROWS[1], ROWS - 1);
// Clear last line
bse::zero<cga_line_t>(SCREEN_ROWS[ROWS - 1]);
}
/*****************************************************************************
* Methode: CGA::clear *
*---------------------------------------------------------------------------*
* Beschreibung: Lösche den Textbildschirm. *
*****************************************************************************/
void CGA::clear() {
/* Hier muess Code eingefuegt werden */
bse::zero<cga_page_t>(SCREEN_PAGE);
setpos(0, 0);
}
/*****************************************************************************
* Methode: CGA::attribute *
*---------------------------------------------------------------------------*
* Beschreibung: Hilfsfunktion zur Erzeugung eines Attribut-Bytes aus *
* Hintergrund- und Vordergrundfarbe und der Angabe, ob das *
* Zeichen blinkend darzustellen ist. *
* *
* Parameter: *
* bg Background color *
* fg Foreground color *
* blink ywa/no *
*****************************************************************************/
unsigned char CGA::attribute(CGA::color bg, CGA::color fg, bool blink) {
/* Hier muess Code eingefuegt werden */
return static_cast<int>(blink) << 7 // B0000000
| (bg & 0x7) << 4 // 0HHH0000 (Hintergrund)
| (fg & 0xF); // 0000VVVV (Vordergrund)
}

108
src/device/graphics/CGA.h Executable file
View File

@ -0,0 +1,108 @@
/*****************************************************************************
* *
* C G A *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Mit Hilfe dieser Klasse kann man auf den Bildschirm des *
* PCs zugreifen. Der Zugriff erfolgt direkt auf der Hard- *
* wareebene, d.h. ueber den Bildschirmspeicher und den *
* I/O-Ports der Grafikkarte. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
* Aenderungen von Michael Schoettner, HHU, 21.8.2016 *
*****************************************************************************/
#ifndef CGA_include_H_
#define CGA_include_H_
#include "device/port/IOport.h"
#include "lib/util/Array.h"
#include "lib/util/Span.h"
#include "lib/util/String.h"
#include "lib/util/StringView.h"
class CGA {
private:
static const IOport index_port; // Auswahl eines Register der Grafikkarte
static const IOport data_port; // Lese-/Schreib-Zugriff auf Register der Grafikk.
public:
// Copy Konstrutkor unterbinden
CGA(const CGA& copy) = delete;
// Konstruktur mit Initialisierung der Ports
CGA() {
CGA::setpos(0, 0);
}
// virtual ~CGA() = default;
// Konstanten fuer die moeglichen Farben im Attribut-Byte.
typedef enum {
BLACK,
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHT_GREY,
DARK_GREY,
LIGHT_BLUE,
LIGHT_GREEN,
LIGHT_CYAN,
LIGHT_RED,
LIGHT_MAGENTA,
YELLOW,
WHITE
} color;
// Standardzeichenfarbe
enum { STD_ATTR = BLACK << 4 | LIGHT_GREY };
// Groesse des Bildschirms (25 Zeilen, 80 Spalten)
enum { ROWS = 25,
COLUMNS = 80 };
// Easier access to memory (also easier copying of lines/pages etc)
struct cga_char_t {
char cga_char;
unsigned char cga_attribute;
};
struct cga_line_t {
// Can use these arrays since they don't have memory overhead (except for the methods that are elsewhere)
bse::array<cga_char_t, COLUMNS> cga_line;
};
struct cga_page_t {
bse::array<cga_line_t, ROWS> cga_page;
};
static const bse::span<cga_char_t, ROWS * COLUMNS> SCREEN;
static const bse::span<cga_line_t, ROWS> SCREEN_ROWS;
static cga_page_t* const SCREEN_PAGE; // No span because can't address anything in [0, 1]
// Setzen des Cursors in Spalte x und Zeile y.
static void setpos(unsigned int x, unsigned int y);
// Abfragen der Cursorpostion
static void getpos(unsigned int& x, unsigned int& y) ;
// Anzeige eines Zeichens mit Attribut an einer bestimmten Stelle
static void show(unsigned int x, unsigned int y, char character, unsigned char attrib = STD_ATTR);
// Anzeige mehrerer Zeichen ab der aktuellen Cursorposition
void print(const bse::string_view substring, unsigned char attrib = STD_ATTR) const;
// Verschiebt den Bildschirminhalt um eine Zeile nach oben.
// Neue Zeile am unteren Bildrand mit Leerzeichen fuellen
virtual void scrollup() const;
// Lösche den Textbildschirm
virtual void clear();
// Hilfsfunktion zur Erzeugung eines Attribut-Bytes
static unsigned char attribute(CGA::color bg, CGA::color fg, bool blink);
};
#endif

View File

@ -0,0 +1,41 @@
/*****************************************************************************
* *
* C G A _ S T R E A M *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Die Klasse CGA_Stream ermoeglicht die Ausgabe verschied. *
* Datentypen als Zeichenketten auf dem CGA-Bildschirm eines*
* PCs. Fuer weitergehende Formatierung oder spezielle *
* Effekte stehen die Methoden der Klasse CGA_Stream zur *
* Verfuegung. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
* Aenderungen von Michael Schoettner, HHU, 1.8.16 *
*****************************************************************************/
#include "CGA_Stream.h"
/*****************************************************************************
* Methode: CGA_Stream::flush *
*---------------------------------------------------------------------------*
* Beschreibung: Methode zur Ausgabe des Pufferinhalts der Basisklasse *
* StringBuffer. Die Methode wird implizit aufgerufen, *
* sobald der Puffer voll ist, kann aber auch explizit *
* verwendet werden, um eine Ausgabe zu erzwingen. *
*****************************************************************************/
void CGA_Stream::flush() {
buffer[pos] = '\0'; // I removed the n argument from print so nullterminate the string
print(buffer.data(), attribute(color_bg, color_fg, blink)); // print(buffer...) would work syntactically
// but the system wouldn't start, as the bse::array
// would be implicitly converted to bse::string and
// that is dynamically allocated.
// print(buffer.data()...) just uses the stack location of
// the internal buffer of bse::array
// Flushing resets attributes
blink = false;
color_bg = CGA::BLACK;
color_fg = CGA::LIGHT_GREY;
pos = 0;
}

View File

@ -0,0 +1,91 @@
/*****************************************************************************
* *
* C G A _ S T R E A M *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Die Klasse CGA_Stream ermoeglicht die Ausgabe verschied. *
* Datentypen als Zeichenketten auf dem CGA-Bildschirm eines*
* PCs. Fuer weitergehende Formatierung oder spezielle *
* Effekte stehen die Methoden der Klasse CGA zur *
* Verfuegung. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
* Aenderungen von Michael Schoettner, HHU, 06.04.20 *
*****************************************************************************/
#ifndef CGA_Stream_include_H_
#define CGA_Stream_include_H_
#include "CGA.h"
#include "lib/stream/OutStream.h"
#include "lib/async/Semaphore.h"
// Allow for easier stream-like color changing
class fgc {
public:
constexpr fgc(const CGA::color fg) : fg(fg) {}
const CGA::color fg;
};
class bgc {
public:
constexpr bgc(const CGA::color bg) : bg(bg) {}
const CGA::color bg;
};
constexpr const fgc white = fgc(CGA::WHITE);
constexpr const fgc black = fgc(CGA::BLACK);
constexpr const fgc green = fgc(CGA::GREEN);
constexpr const fgc red = fgc(CGA::RED);
constexpr const fgc lgrey = fgc(CGA::LIGHT_GREY);
class CGA_Stream : public OutStream, public CGA {
private:
// Allow for synchronization of output text, needed when running something in parallel to
// the PreemptiveThreadDemo for example
// NOTE: Should only be used by threads (like the demos) to not lock the system
Semaphore sem;
CGA::color color_fg;
CGA::color color_bg;
bool blink;
friend class Logger; // Give access to the color
public:
CGA_Stream(CGA_Stream& copy) = delete; // Verhindere Kopieren
CGA_Stream() : sem(1), color_fg(CGA::LIGHT_GREY), color_bg(CGA::BLACK), blink(false) {
pos = 0;
}
// CAn't make singleton because atexit
// ~CGA_Stream() override = default;
void lock() { sem.p(); }
void unlock() { sem.v(); }
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
void flush() override;
// Change stream color
template<typename T>
friend T& operator<<(T& os, const fgc& fg) {
CGA::color old_bg = os.color_bg;
os.flush();
os.color_bg = old_bg;
os.color_fg = fg.fg;
return os;
}
template<typename T>
friend T& operator<<(T& os, const bgc& bg) {
CGA::color old_fg = os.color_fg;
os.flush();
os.color_fg = old_fg;
os.color_bg = bg.bg;
return os;
}
};
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,264 @@
// vim: set et ts=4 sw=4:
/* Acorn-like font definition, with PC graphics characters */
constexpr const unsigned char acorndata_8x8[] = {
/* 00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ^@ */
/* 01 */ 0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e, /* ^A */
/* 02 */ 0x7e, 0xff, 0xbd, 0xff, 0xc3, 0xe7, 0xff, 0x7e, /* ^B */
/* 03 */ 0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00, /* ^C */
/* 04 */ 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x10, 0x00, /* ^D */
/* 05 */ 0x00, 0x18, 0x3c, 0xe7, 0xe7, 0x3c, 0x18, 0x00, /* ^E */
/* 06 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 07 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 09 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 0A */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 0B */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 0C */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 0D */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 0E */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 0F */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 10 */ 0x00, 0x60, 0x78, 0x7e, 0x7e, 0x78, 0x60, 0x00, /* |> */
/* 11 */ 0x00, 0x06, 0x1e, 0x7e, 0x7e, 0x1e, 0x06, 0x00, /* <| */
/* 12 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 13 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 14 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 15 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 16 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 17 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 19 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 1A */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 1B */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 1C */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 1D */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 1E */ 0x00, 0x18, 0x18, 0x3c, 0x3c, 0x7e, 0x7e, 0x00, /* /\ */
/* 1F */ 0x00, 0x7e, 0x7e, 0x3c, 0x3c, 0x18, 0x18, 0x00, /* \/ */
/* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* */
/* 21 */ 0x18, 0x3c, 0x3c, 0x18, 0x18, 0x00, 0x18, 0x00, /* ! */
/* 22 */ 0x6C, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, /* " */
/* 23 */ 0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00, /* # */
/* 24 */ 0x0C, 0x3F, 0x68, 0x3E, 0x0B, 0x7E, 0x18, 0x00, /* $ */
/* 25 */ 0x60, 0x66, 0x0C, 0x18, 0x30, 0x66, 0x06, 0x00, /* % */
/* 26 */ 0x38, 0x6C, 0x6C, 0x38, 0x6D, 0x66, 0x3B, 0x00, /* & */
/* 27 */ 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, /* ' */
/* 28 */ 0x0C, 0x18, 0x30, 0x30, 0x30, 0x18, 0x0C, 0x00, /* ( */
/* 29 */ 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x18, 0x30, 0x00, /* ) */
/* 2A */ 0x00, 0x18, 0x7E, 0x3C, 0x7E, 0x18, 0x00, 0x00, /* * */
/* 2B */ 0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00, /* + */
/* 2C */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, /* , */
/* 2D */ 0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00, /* - */
/* 2E */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, /* . */
/* 2F */ 0x00, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x00, 0x00, /* / */
/* 30 */ 0x3C, 0x66, 0x6E, 0x7E, 0x76, 0x66, 0x3C, 0x00, /* 0 */
/* 31 */ 0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, /* 1 */
/* 32 */ 0x3C, 0x66, 0x06, 0x0C, 0x18, 0x30, 0x7E, 0x00, /* 2 */
/* 33 */ 0x3C, 0x66, 0x06, 0x1C, 0x06, 0x66, 0x3C, 0x00, /* 3 */
/* 34 */ 0x0C, 0x1C, 0x3C, 0x6C, 0x7E, 0x0C, 0x0C, 0x00, /* 4 */
/* 35 */ 0x7E, 0x60, 0x7C, 0x06, 0x06, 0x66, 0x3C, 0x00, /* 5 */
/* 36 */ 0x1C, 0x30, 0x60, 0x7C, 0x66, 0x66, 0x3C, 0x00, /* 6 */
/* 37 */ 0x7E, 0x06, 0x0C, 0x18, 0x30, 0x30, 0x30, 0x00, /* 7 */
/* 38 */ 0x3C, 0x66, 0x66, 0x3C, 0x66, 0x66, 0x3C, 0x00, /* 8 */
/* 39 */ 0x3C, 0x66, 0x66, 0x3E, 0x06, 0x0C, 0x38, 0x00, /* 9 */
/* 3A */ 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, /* : */
/* 3B */ 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x30, /* ; */
/* 3C */ 0x0C, 0x18, 0x30, 0x60, 0x30, 0x18, 0x0C, 0x00, /* < */
/* 3D */ 0x00, 0x00, 0x7E, 0x00, 0x7E, 0x00, 0x00, 0x00, /* = */
/* 3E */ 0x30, 0x18, 0x0C, 0x06, 0x0C, 0x18, 0x30, 0x00, /* > */
/* 3F */ 0x3C, 0x66, 0x0C, 0x18, 0x18, 0x00, 0x18, 0x00, /* ? */
/* 40 */ 0x3C, 0x66, 0x6E, 0x6A, 0x6E, 0x60, 0x3C, 0x00, /* @ */
/* 41 */ 0x3C, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x66, 0x00, /* A */
/* 42 */ 0x7C, 0x66, 0x66, 0x7C, 0x66, 0x66, 0x7C, 0x00, /* B */
/* 43 */ 0x3C, 0x66, 0x60, 0x60, 0x60, 0x66, 0x3C, 0x00, /* C */
/* 44 */ 0x78, 0x6C, 0x66, 0x66, 0x66, 0x6C, 0x78, 0x00, /* D */
/* 45 */ 0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x7E, 0x00, /* E */
/* 46 */ 0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x60, 0x00, /* F */
/* 47 */ 0x3C, 0x66, 0x60, 0x6E, 0x66, 0x66, 0x3C, 0x00, /* G */
/* 48 */ 0x66, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x66, 0x00, /* H */
/* 49 */ 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, /* I */
/* 4A */ 0x3E, 0x0C, 0x0C, 0x0C, 0x0C, 0x6C, 0x38, 0x00, /* J */
/* 4B */ 0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66, 0x00, /* K */
/* 4C */ 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7E, 0x00, /* L */
/* 4D */ 0x63, 0x77, 0x7F, 0x6B, 0x6B, 0x63, 0x63, 0x00, /* M */
/* 4E */ 0x66, 0x66, 0x76, 0x7E, 0x6E, 0x66, 0x66, 0x00, /* N */
/* 4F */ 0x3C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00, /* O */
/* 50 */ 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60, 0x60, 0x00, /* P */
/* 51 */ 0x3C, 0x66, 0x66, 0x66, 0x6A, 0x6C, 0x36, 0x00, /* Q */
/* 52 */ 0x7C, 0x66, 0x66, 0x7C, 0x6C, 0x66, 0x66, 0x00, /* R */
/* 53 */ 0x3C, 0x66, 0x60, 0x3C, 0x06, 0x66, 0x3C, 0x00, /* S */
/* 54 */ 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, /* T */
/* 55 */ 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00, /* U */
/* 56 */ 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00, /* V */
/* 57 */ 0x63, 0x63, 0x6B, 0x6B, 0x7F, 0x77, 0x63, 0x00, /* W */
/* 58 */ 0x66, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x66, 0x00, /* X */
/* 59 */ 0x66, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x00, /* Y */
/* 5A */ 0x7E, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x7E, 0x00, /* Z */
/* 5B */ 0x7C, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7C, 0x00, /* [ */
/* 5C */ 0x00, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x00, 0x00, /* \ */
/* 5D */ 0x3E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x3E, 0x00, /* ] */
/* 5E */ 0x3C, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ^ */
/* 5F */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, /* _ */
/* 60 */ 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* */
/* 61 */ 0x00, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00, /* a */
/* 62 */ 0x60, 0x60, 0x7C, 0x66, 0x66, 0x66, 0x7C, 0x00, /* b */
/* 63 */ 0x00, 0x00, 0x3C, 0x66, 0x60, 0x66, 0x3C, 0x00, /* c */
/* 64 */ 0x06, 0x06, 0x3E, 0x66, 0x66, 0x66, 0x3E, 0x00, /* d */
/* 65 */ 0x00, 0x00, 0x3C, 0x66, 0x7E, 0x60, 0x3C, 0x00, /* e */
/* 66 */ 0x1C, 0x30, 0x30, 0x7C, 0x30, 0x30, 0x30, 0x00, /* f */
/* 67 */ 0x00, 0x00, 0x3E, 0x66, 0x66, 0x3E, 0x06, 0x3C, /* g */
/* 68 */ 0x60, 0x60, 0x7C, 0x66, 0x66, 0x66, 0x66, 0x00, /* h */
/* 69 */ 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x3C, 0x00, /* i */
/* 6A */ 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x70, /* j */
/* 6B */ 0x60, 0x60, 0x66, 0x6C, 0x78, 0x6C, 0x66, 0x00, /* k */
/* 6C */ 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, /* l */
/* 6D */ 0x00, 0x00, 0x36, 0x7F, 0x6B, 0x6B, 0x63, 0x00, /* m */
/* 6E */ 0x00, 0x00, 0x7C, 0x66, 0x66, 0x66, 0x66, 0x00, /* n */
/* 6F */ 0x00, 0x00, 0x3C, 0x66, 0x66, 0x66, 0x3C, 0x00, /* o */
/* 70 */ 0x00, 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60, /* p */
/* 71 */ 0x00, 0x00, 0x3E, 0x66, 0x66, 0x3E, 0x06, 0x07, /* q */
/* 72 */ 0x00, 0x00, 0x6C, 0x76, 0x60, 0x60, 0x60, 0x00, /* r */
/* 73 */ 0x00, 0x00, 0x3E, 0x60, 0x3C, 0x06, 0x7C, 0x00, /* s */
/* 74 */ 0x30, 0x30, 0x7C, 0x30, 0x30, 0x30, 0x1C, 0x00, /* t */
/* 75 */ 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3E, 0x00, /* u */
/* 76 */ 0x00, 0x00, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00, /* v */
/* 77 */ 0x00, 0x00, 0x63, 0x6B, 0x6B, 0x7F, 0x36, 0x00, /* w */
/* 78 */ 0x00, 0x00, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x00, /* x */
/* 79 */ 0x00, 0x00, 0x66, 0x66, 0x66, 0x3E, 0x06, 0x3C, /* y */
/* 7A */ 0x00, 0x00, 0x7E, 0x0C, 0x18, 0x30, 0x7E, 0x00, /* z */
/* 7B */ 0x0C, 0x18, 0x18, 0x70, 0x18, 0x18, 0x0C, 0x00, /* { */
/* 7C */ 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, /* | */
/* 7D */ 0x30, 0x18, 0x18, 0x0E, 0x18, 0x18, 0x30, 0x00, /* } */
/* 7E */ 0x31, 0x6B, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, /* ~ */
/* 7F */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /*  */
/* 80 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 81 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 82 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 83 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 84 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 85 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 86 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 87 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 88 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 89 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8A */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8B */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8C */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8D */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8E */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 8F */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 90 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 91 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 92 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 93 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 94 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 95 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 96 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 97 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 98 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 99 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9A */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9B */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9C */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9D */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9E */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* 9F */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A2 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A3 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A4 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A5 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A6 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A7 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* A9 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* AA */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* AB */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* AC */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* AD */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* AE */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* AF */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* B0 */ 0x22, 0x88, 0x22, 0x88, 0x22, 0x88, 0x22, 0x88,
/* B1 */ 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa,
/* B2 */ 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77,
/* B3 */ 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
/* B4 */ 0x18, 0x18, 0x18, 0xf8, 0x18, 0x18, 0x18, 0x18,
/* B5 */ 0x18, 0x18, 0xf8, 0x18, 0xf8, 0x18, 0x18, 0x18,
/* B6 */ 0x66, 0x66, 0x66, 0xe6, 0x66, 0x66, 0x66, 0x66,
/* B7 */ 0x00, 0x00, 0x00, 0xfe, 0x66, 0x66, 0x66, 0x66,
/* B8 */ 0x00, 0x00, 0xf8, 0x18, 0xf8, 0x18, 0x18, 0x18,
/* B9 */ 0x66, 0x66, 0xe6, 0x06, 0xe6, 0x66, 0x66, 0x66,
/* BA */ 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
/* BB */ 0x00, 0x00, 0xfe, 0x06, 0xe6, 0x66, 0x66, 0x66,
/* BC */ 0x66, 0x66, 0xe6, 0x06, 0xfe, 0x00, 0x00, 0x00,
/* BD */ 0x66, 0x66, 0x66, 0xfe, 0x00, 0x00, 0x00, 0x00,
/* BE */ 0x18, 0x18, 0xf8, 0x18, 0xf8, 0x00, 0x00, 0x00,
/* BF */ 0x00, 0x00, 0x00, 0xf8, 0x18, 0x18, 0x18, 0x18,
/* C0 */ 0x18, 0x18, 0x18, 0x1f, 0x00, 0x00, 0x00, 0x00,
/* C1 */ 0x18, 0x18, 0x18, 0xff, 0x00, 0x00, 0x00, 0x00,
/* C2 */ 0x00, 0x00, 0x00, 0xff, 0x18, 0x18, 0x18, 0x18,
/* C3 */ 0x18, 0x18, 0x18, 0x1f, 0x18, 0x18, 0x18, 0x18,
/* C4 */ 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
/* C5 */ 0x18, 0x18, 0x18, 0xff, 0x18, 0x18, 0x18, 0x18,
/* C6 */ 0x18, 0x18, 0x1f, 0x18, 0x1f, 0x18, 0x18, 0x18,
/* C7 */ 0x66, 0x66, 0x66, 0x67, 0x66, 0x66, 0x66, 0x66,
/* C8 */ 0x66, 0x66, 0x67, 0x60, 0x7f, 0x00, 0x00, 0x00,
/* C9 */ 0x00, 0x00, 0x7f, 0x60, 0x67, 0x66, 0x66, 0x66,
/* CA */ 0x66, 0x66, 0xe7, 0x00, 0xff, 0x00, 0x00, 0x00,
/* CB */ 0x00, 0x00, 0xff, 0x00, 0xe7, 0x66, 0x66, 0x66,
/* CC */ 0x66, 0x66, 0x67, 0x60, 0x67, 0x66, 0x66, 0x66,
/* CD */ 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00,
/* CE */ 0x66, 0x66, 0xe7, 0x00, 0xe7, 0x66, 0x66, 0x66,
/* CF */ 0x18, 0x18, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00,
/* D0 */ 0x66, 0x66, 0x66, 0xff, 0x00, 0x00, 0x00, 0x00,
/* D1 */ 0x00, 0x00, 0xff, 0x00, 0xff, 0x18, 0x18, 0x18,
/* D2 */ 0x00, 0x00, 0x00, 0xff, 0x66, 0x66, 0x66, 0x66,
/* D3 */ 0x66, 0x66, 0x66, 0x7f, 0x00, 0x00, 0x00, 0x00,
/* D4 */ 0x18, 0x18, 0x1f, 0x18, 0x1f, 0x00, 0x00, 0x00,
/* D5 */ 0x00, 0x00, 0x1f, 0x18, 0x1f, 0x18, 0x18, 0x18,
/* D6 */ 0x00, 0x00, 0x00, 0x7f, 0x66, 0x66, 0x66, 0x66,
/* D7 */ 0x66, 0x66, 0x66, 0xff, 0x66, 0x66, 0x66, 0x66,
/* D8 */ 0x18, 0x18, 0xff, 0x18, 0xff, 0x18, 0x18, 0x18,
/* D9 */ 0x18, 0x18, 0x18, 0xf8, 0x00, 0x00, 0x00, 0x00,
/* DA */ 0x00, 0x00, 0x00, 0x1f, 0x18, 0x18, 0x18, 0x18,
/* DB */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
/* DC */ 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
/* DD */ 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
/* DE */ 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
/* DF */ 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
/* E0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E2 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E3 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E4 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E5 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E6 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E7 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* E9 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* EA */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* EB */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* EC */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* ED */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* EE */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* EF */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F2 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F3 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F4 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F5 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F6 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F7 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* F9 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* FA */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* FB */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* FC */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* FD */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* FE */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* FF */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#undef FONTDATAMAX

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,265 @@
// vim: set et ts=4 sw=4:
constexpr const unsigned int FONTDATAMAX_SUN8x16 = 4096;
constexpr const unsigned char fontdata_sun_8x16[FONTDATAMAX_SUN8x16] = {
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x7e,0x81,0xa5,0x81,0x81,0xbd,0x99,0x81,0x81,0x7e,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x7e,0xff,0xdb,0xff,0xff,0xc3,0xe7,0xff,0xff,0x7e,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x6c,0xfe,0xfe,0xfe,0xfe,0x7c,0x38,0x10,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x10,0x38,0x7c,0xfe,0x7c,0x38,0x10,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x18,0x3c,0x3c,0xe7,0xe7,0xe7,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x18,0x3c,0x7e,0xff,0xff,0x7e,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x3c,0x3c,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xc3,0xc3,0xe7,0xff,0xff,0xff,0xff,0xff,0xff,
/* */ 0x00,0x00,0x00,0x00,0x00,0x3c,0x66,0x42,0x42,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,
/* */ 0xff,0xff,0xff,0xff,0xff,0xc3,0x99,0xbd,0xbd,0x99,0xc3,0xff,0xff,0xff,0xff,0xff,
/* */ 0x00,0x00,0x1e,0x0e,0x1a,0x32,0x78,0xcc,0xcc,0xcc,0xcc,0x78,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x3c,0x66,0x66,0x66,0x66,0x3c,0x18,0x7e,0x18,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x3f,0x33,0x3f,0x30,0x30,0x30,0x30,0x70,0xf0,0xe0,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x7f,0x63,0x7f,0x63,0x63,0x63,0x63,0x67,0xe7,0xe6,0xc0,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x18,0x18,0xdb,0x3c,0xe7,0x3c,0xdb,0x18,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfe,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,
/* */ 0x00,0x02,0x06,0x0e,0x1e,0x3e,0xfe,0x3e,0x1e,0x0e,0x06,0x02,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x18,0x3c,0x7e,0x18,0x18,0x18,0x7e,0x3c,0x18,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x00,0x66,0x66,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x7f,0xdb,0xdb,0xdb,0x7b,0x1b,0x1b,0x1b,0x1b,0x1b,0x00,0x00,0x00,0x00,
/* */ 0x00,0x7c,0xc6,0x60,0x38,0x6c,0xc6,0xc6,0x6c,0x38,0x0c,0xc6,0x7c,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xfe,0xfe,0xfe,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x18,0x3c,0x7e,0x18,0x18,0x18,0x7e,0x3c,0x18,0x7e,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x18,0x3c,0x7e,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x7e,0x3c,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x18,0x0c,0xfe,0x0c,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x30,0x60,0xfe,0x60,0x30,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0xc0,0xc0,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x24,0x66,0xff,0x66,0x24,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x10,0x38,0x38,0x7c,0x7c,0xfe,0xfe,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0xfe,0xfe,0x7c,0x7c,0x38,0x38,0x10,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*!*/ 0x00,0x00,0x18,0x3c,0x3c,0x3c,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00,
/*"*/ 0x00,0x66,0x66,0x66,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*#*/ 0x00,0x00,0x00,0x6c,0x6c,0xfe,0x6c,0x6c,0x6c,0xfe,0x6c,0x6c,0x00,0x00,0x00,0x00,
/*$*/ 0x18,0x18,0x7c,0xc6,0xc2,0xc0,0x7c,0x06,0x06,0x86,0xc6,0x7c,0x18,0x18,0x00,0x00,
/*%*/ 0x00,0x00,0x00,0x00,0xc2,0xc6,0x0c,0x18,0x30,0x60,0xc6,0x86,0x00,0x00,0x00,0x00,
/*&*/ 0x00,0x00,0x38,0x6c,0x6c,0x38,0x76,0xdc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/*'*/ 0x00,0x30,0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*(*/ 0x00,0x00,0x0c,0x18,0x30,0x30,0x30,0x30,0x30,0x30,0x18,0x0c,0x00,0x00,0x00,0x00,
/*)*/ 0x00,0x00,0x30,0x18,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x18,0x30,0x00,0x00,0x00,0x00,
/***/ 0x00,0x00,0x00,0x00,0x00,0x66,0x3c,0xff,0x3c,0x66,0x00,0x00,0x00,0x00,0x00,0x00,
/*+*/ 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x7e,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
/*,*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x30,0x00,0x00,0x00,
/*-*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*.*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x02,0x06,0x0c,0x18,0x30,0x60,0xc0,0x80,0x00,0x00,0x00,0x00,
/*0*/ 0x00,0x00,0x7c,0xc6,0xc6,0xce,0xde,0xf6,0xe6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*1*/ 0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,0x00,0x00,
/*2*/ 0x00,0x00,0x7c,0xc6,0x06,0x0c,0x18,0x30,0x60,0xc0,0xc6,0xfe,0x00,0x00,0x00,0x00,
/*3*/ 0x00,0x00,0x7c,0xc6,0x06,0x06,0x3c,0x06,0x06,0x06,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*4*/ 0x00,0x00,0x0c,0x1c,0x3c,0x6c,0xcc,0xfe,0x0c,0x0c,0x0c,0x1e,0x00,0x00,0x00,0x00,
/*5*/ 0x00,0x00,0xfe,0xc0,0xc0,0xc0,0xfc,0x06,0x06,0x06,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*6*/ 0x00,0x00,0x38,0x60,0xc0,0xc0,0xfc,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*7*/ 0x00,0x00,0xfe,0xc6,0x06,0x06,0x0c,0x18,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,
/*8*/ 0x00,0x00,0x7c,0xc6,0xc6,0xc6,0x7c,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*9*/ 0x00,0x00,0x7c,0xc6,0xc6,0xc6,0x7e,0x06,0x06,0x06,0x0c,0x78,0x00,0x00,0x00,0x00,
/*:*/ 0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,
/*;*/ 0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00,
/*<*/ 0x00,0x00,0x00,0x06,0x0c,0x18,0x30,0x60,0x30,0x18,0x0c,0x06,0x00,0x00,0x00,0x00,
/*=*/ 0x00,0x00,0x00,0x00,0x00,0x7e,0x00,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*>*/ 0x00,0x00,0x00,0x60,0x30,0x18,0x0c,0x06,0x0c,0x18,0x30,0x60,0x00,0x00,0x00,0x00,
/*?*/ 0x00,0x00,0x7c,0xc6,0xc6,0x0c,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00,
/*@*/ 0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xde,0xde,0xde,0xdc,0xc0,0x7c,0x00,0x00,0x00,0x00,
/*A*/ 0x00,0x00,0x10,0x38,0x6c,0xc6,0xc6,0xfe,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00,
/*B*/ 0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x66,0x66,0x66,0x66,0xfc,0x00,0x00,0x00,0x00,
/*C*/ 0x00,0x00,0x3c,0x66,0xc2,0xc0,0xc0,0xc0,0xc0,0xc2,0x66,0x3c,0x00,0x00,0x00,0x00,
/*D*/ 0x00,0x00,0xf8,0x6c,0x66,0x66,0x66,0x66,0x66,0x66,0x6c,0xf8,0x00,0x00,0x00,0x00,
/*E*/ 0x00,0x00,0xfe,0x66,0x62,0x68,0x78,0x68,0x60,0x62,0x66,0xfe,0x00,0x00,0x00,0x00,
/*F*/ 0x00,0x00,0xfe,0x66,0x62,0x68,0x78,0x68,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00,
/*G*/ 0x00,0x00,0x3c,0x66,0xc2,0xc0,0xc0,0xde,0xc6,0xc6,0x66,0x3a,0x00,0x00,0x00,0x00,
/*H*/ 0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xfe,0xc6,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00,
/*I*/ 0x00,0x00,0x3c,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/*J*/ 0x00,0x00,0x1e,0x0c,0x0c,0x0c,0x0c,0x0c,0xcc,0xcc,0xcc,0x78,0x00,0x00,0x00,0x00,
/*K*/ 0x00,0x00,0xe6,0x66,0x66,0x6c,0x78,0x78,0x6c,0x66,0x66,0xe6,0x00,0x00,0x00,0x00,
/*L*/ 0x00,0x00,0xf0,0x60,0x60,0x60,0x60,0x60,0x60,0x62,0x66,0xfe,0x00,0x00,0x00,0x00,
/*M*/ 0x00,0x00,0xc3,0xe7,0xff,0xff,0xdb,0xc3,0xc3,0xc3,0xc3,0xc3,0x00,0x00,0x00,0x00,
/*N*/ 0x00,0x00,0xc6,0xe6,0xf6,0xfe,0xde,0xce,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00,
/*O*/ 0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*P*/ 0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x60,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00,
/*Q*/ 0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xd6,0xde,0x7c,0x0c,0x0e,0x00,0x00,
/*R*/ 0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x6c,0x66,0x66,0x66,0xe6,0x00,0x00,0x00,0x00,
/*S*/ 0x00,0x00,0x7c,0xc6,0xc6,0x60,0x38,0x0c,0x06,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*T*/ 0x00,0x00,0xff,0xdb,0x99,0x18,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/*U*/ 0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*V*/ 0x00,0x00,0xc3,0xc3,0xc3,0xc3,0xc3,0xc3,0xc3,0x66,0x3c,0x18,0x00,0x00,0x00,0x00,
/*W*/ 0x00,0x00,0xc3,0xc3,0xc3,0xc3,0xc3,0xdb,0xdb,0xff,0x66,0x66,0x00,0x00,0x00,0x00,
/*X*/ 0x00,0x00,0xc3,0xc3,0x66,0x3c,0x18,0x18,0x3c,0x66,0xc3,0xc3,0x00,0x00,0x00,0x00,
/*Y*/ 0x00,0x00,0xc3,0xc3,0xc3,0x66,0x3c,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/*Z*/ 0x00,0x00,0xff,0xc3,0x86,0x0c,0x18,0x30,0x60,0xc1,0xc3,0xff,0x00,0x00,0x00,0x00,
/*[*/ 0x00,0x00,0x3c,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3c,0x00,0x00,0x00,0x00,
/*\*/ 0x00,0x00,0x00,0x80,0xc0,0xe0,0x70,0x38,0x1c,0x0e,0x06,0x02,0x00,0x00,0x00,0x00,
/*]*/ 0x00,0x00,0x3c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3c,0x00,0x00,0x00,0x00,
/*^*/ 0x10,0x38,0x6c,0xc6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*_*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0x00,
/* */ 0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*a*/ 0x00,0x00,0x00,0x00,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/*b*/ 0x00,0x00,0xe0,0x60,0x60,0x78,0x6c,0x66,0x66,0x66,0x66,0x7c,0x00,0x00,0x00,0x00,
/*c*/ 0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xc0,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*d*/ 0x00,0x00,0x1c,0x0c,0x0c,0x3c,0x6c,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/*e*/ 0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xfe,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*f*/ 0x00,0x00,0x38,0x6c,0x64,0x60,0xf0,0x60,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00,
/*g*/ 0x00,0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0xcc,0xcc,0x7c,0x0c,0xcc,0x78,0x00,
/*h*/ 0x00,0x00,0xe0,0x60,0x60,0x6c,0x76,0x66,0x66,0x66,0x66,0xe6,0x00,0x00,0x00,0x00,
/*i*/ 0x00,0x00,0x18,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/*j*/ 0x00,0x00,0x06,0x06,0x00,0x0e,0x06,0x06,0x06,0x06,0x06,0x06,0x66,0x66,0x3c,0x00,
/*k*/ 0x00,0x00,0xe0,0x60,0x60,0x66,0x6c,0x78,0x78,0x6c,0x66,0xe6,0x00,0x00,0x00,0x00,
/*l*/ 0x00,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/*m*/ 0x00,0x00,0x00,0x00,0x00,0xe6,0xff,0xdb,0xdb,0xdb,0xdb,0xdb,0x00,0x00,0x00,0x00,
/*n*/ 0x00,0x00,0x00,0x00,0x00,0xdc,0x66,0x66,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00,
/*o*/ 0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*p*/ 0x00,0x00,0x00,0x00,0x00,0xdc,0x66,0x66,0x66,0x66,0x66,0x7c,0x60,0x60,0xf0,0x00,
/*q*/ 0x00,0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0xcc,0xcc,0x7c,0x0c,0x0c,0x1e,0x00,
/*r*/ 0x00,0x00,0x00,0x00,0x00,0xdc,0x76,0x66,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00,
/*s*/ 0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0x60,0x38,0x0c,0xc6,0x7c,0x00,0x00,0x00,0x00,
/*t*/ 0x00,0x00,0x10,0x30,0x30,0xfc,0x30,0x30,0x30,0x30,0x36,0x1c,0x00,0x00,0x00,0x00,
/*u*/ 0x00,0x00,0x00,0x00,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/*v*/ 0x00,0x00,0x00,0x00,0x00,0xc3,0xc3,0xc3,0xc3,0x66,0x3c,0x18,0x00,0x00,0x00,0x00,
/*w*/ 0x00,0x00,0x00,0x00,0x00,0xc3,0xc3,0xc3,0xdb,0xdb,0xff,0x66,0x00,0x00,0x00,0x00,
/*x*/ 0x00,0x00,0x00,0x00,0x00,0xc3,0x66,0x3c,0x18,0x3c,0x66,0xc3,0x00,0x00,0x00,0x00,
/*y*/ 0x00,0x00,0x00,0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7e,0x06,0x0c,0xf8,0x00,
/*z*/ 0x00,0x00,0x00,0x00,0x00,0xfe,0xcc,0x18,0x30,0x60,0xc6,0xfe,0x00,0x00,0x00,0x00,
/*{*/ 0x00,0x00,0x0e,0x18,0x18,0x18,0x70,0x18,0x18,0x18,0x18,0x0e,0x00,0x00,0x00,0x00,
/*|*/ 0x00,0x00,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,
/*}*/ 0x00,0x00,0x70,0x18,0x18,0x18,0x0e,0x18,0x18,0x18,0x18,0x70,0x00,0x00,0x00,0x00,
/*~*/ 0x00,0x00,0x76,0xdc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x10,0x38,0x6c,0xc6,0xc6,0xc6,0xfe,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x3c,0x66,0xc2,0xc0,0xc0,0xc0,0xc2,0x66,0x3c,0x0c,0x06,0x7c,0x00,0x00,
/* */ 0x00,0x00,0xcc,0x00,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x0c,0x18,0x30,0x00,0x7c,0xc6,0xfe,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x10,0x38,0x6c,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0xcc,0x00,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x60,0x30,0x18,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x38,0x6c,0x38,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x3c,0x66,0x60,0x60,0x66,0x3c,0x0c,0x06,0x3c,0x00,0x00,0x00,
/* */ 0x00,0x10,0x38,0x6c,0x00,0x7c,0xc6,0xfe,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0xc6,0x00,0x00,0x7c,0xc6,0xfe,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x60,0x30,0x18,0x00,0x7c,0xc6,0xfe,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x66,0x00,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x18,0x3c,0x66,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x60,0x30,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/* */ 0x00,0xc6,0x00,0x10,0x38,0x6c,0xc6,0xc6,0xfe,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00,
/* */ 0x38,0x6c,0x38,0x00,0x38,0x6c,0xc6,0xc6,0xfe,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00,
/* */ 0x18,0x30,0x60,0x00,0xfe,0x66,0x60,0x7c,0x60,0x60,0x66,0xfe,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x6e,0x3b,0x1b,0x7e,0xd8,0xdc,0x77,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x3e,0x6c,0xcc,0xcc,0xfe,0xcc,0xcc,0xcc,0xcc,0xce,0x00,0x00,0x00,0x00,
/* */ 0x00,0x10,0x38,0x6c,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0xc6,0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x60,0x30,0x18,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x30,0x78,0xcc,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x60,0x30,0x18,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0xc6,0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7e,0x06,0x0c,0x78,0x00,
/* */ 0x00,0xc6,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0xc6,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x18,0x18,0x7e,0xc3,0xc0,0xc0,0xc0,0xc3,0x7e,0x18,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x38,0x6c,0x64,0x60,0xf0,0x60,0x60,0x60,0x60,0xe6,0xfc,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0xc3,0x66,0x3c,0x18,0xff,0x18,0xff,0x18,0x18,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0xfc,0x66,0x66,0x7c,0x62,0x66,0x6f,0x66,0x66,0x66,0xf3,0x00,0x00,0x00,0x00,
/* */ 0x00,0x0e,0x1b,0x18,0x18,0x18,0x7e,0x18,0x18,0x18,0x18,0x18,0xd8,0x70,0x00,0x00,
/* */ 0x00,0x18,0x30,0x60,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x0c,0x18,0x30,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x18,0x30,0x60,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x18,0x30,0x60,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x76,0xdc,0x00,0xdc,0x66,0x66,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00,
/* */ 0x76,0xdc,0x00,0xc6,0xe6,0xf6,0xfe,0xde,0xce,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00,
/* */ 0x00,0x3c,0x6c,0x6c,0x3e,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x38,0x6c,0x6c,0x38,0x00,0x7c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x30,0x30,0x00,0x30,0x30,0x60,0xc0,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xc0,0xc0,0xc0,0xc0,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0x06,0x06,0x06,0x06,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0xc0,0xc0,0xc2,0xc6,0xcc,0x18,0x30,0x60,0xce,0x9b,0x06,0x0c,0x1f,0x00,0x00,
/* */ 0x00,0xc0,0xc0,0xc2,0xc6,0xcc,0x18,0x30,0x66,0xce,0x96,0x3e,0x06,0x06,0x00,0x00,
/* */ 0x00,0x00,0x18,0x18,0x00,0x18,0x18,0x18,0x3c,0x3c,0x3c,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x36,0x6c,0xd8,0x6c,0x36,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0xd8,0x6c,0x36,0x6c,0xd8,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x11,0x44,0x11,0x44,0x11,0x44,0x11,0x44,0x11,0x44,0x11,0x44,0x11,0x44,0x11,0x44,
/* */ 0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,
/* */ 0xdd,0x77,0xdd,0x77,0xdd,0x77,0xdd,0x77,0xdd,0x77,0xdd,0x77,0xdd,0x77,0xdd,0x77,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xf8,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x18,0x18,0x18,0x18,0x18,0xf8,0x18,0xf8,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0xf6,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x00,0x00,0x00,0x00,0x00,0xf8,0x18,0xf8,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x36,0x36,0x36,0x36,0x36,0xf6,0x06,0xf6,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x00,0x00,0x00,0x00,0x00,0xfe,0x06,0xf6,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x36,0x36,0x36,0x36,0x36,0xf6,0x06,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x18,0x18,0x18,0x18,0x18,0xf8,0x18,0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x1f,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x18,0x18,0x18,0x18,0x18,0x1f,0x18,0x1f,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x37,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x36,0x36,0x36,0x36,0x36,0x37,0x30,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x3f,0x30,0x37,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x36,0x36,0x36,0x36,0x36,0xf7,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0xff,0x00,0xf7,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x36,0x36,0x36,0x36,0x36,0x37,0x30,0x37,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x00,0x00,0x00,0x00,0x00,0xff,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x36,0x36,0x36,0x36,0x36,0xf7,0x00,0xf7,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x18,0x18,0x18,0x18,0x18,0xff,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0xff,0x00,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x18,0x18,0x18,0x18,0x18,0x1f,0x18,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x1f,0x18,0x1f,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3f,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0xff,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
/* */ 0x18,0x18,0x18,0x18,0x18,0xff,0x18,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
/* */ 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,
/* */ 0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,
/* */ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x76,0xdc,0xd8,0xd8,0xd8,0xdc,0x76,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x78,0xcc,0xcc,0xcc,0xd8,0xcc,0xc6,0xc6,0xc6,0xcc,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0xfe,0xc6,0xc6,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0xfe,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x6c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0xfe,0xc6,0x60,0x30,0x18,0x30,0x60,0xc6,0xfe,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x7e,0xd8,0xd8,0xd8,0xd8,0xd8,0x70,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x7c,0x60,0x60,0xc0,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x76,0xdc,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x7e,0x18,0x3c,0x66,0x66,0x66,0x3c,0x18,0x7e,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x38,0x6c,0xc6,0xc6,0xfe,0xc6,0xc6,0x6c,0x38,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x38,0x6c,0xc6,0xc6,0xc6,0x6c,0x6c,0x6c,0x6c,0xee,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x1e,0x30,0x18,0x0c,0x3e,0x66,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x7e,0xdb,0xdb,0xdb,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x03,0x06,0x7e,0xdb,0xdb,0xf3,0x7e,0x60,0xc0,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x1c,0x30,0x60,0x60,0x7c,0x60,0x60,0x60,0x30,0x1c,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0xfe,0x00,0x00,0xfe,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x18,0x18,0x7e,0x18,0x18,0x00,0x00,0xff,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x30,0x18,0x0c,0x06,0x0c,0x18,0x30,0x00,0x7e,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x0c,0x18,0x30,0x60,0x30,0x18,0x0c,0x00,0x7e,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x0e,0x1b,0x1b,0x1b,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
/* */ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xd8,0xd8,0xd8,0x70,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x7e,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x76,0xdc,0x00,0x76,0xdc,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x38,0x6c,0x6c,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x0f,0x0c,0x0c,0x0c,0x0c,0x0c,0xec,0x6c,0x6c,0x3c,0x1c,0x00,0x00,0x00,0x00,
/* */ 0x00,0xd8,0x6c,0x6c,0x6c,0x6c,0x6c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x70,0xd8,0x30,0x60,0xc8,0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x7c,0x7c,0x7c,0x7c,0x7c,0x7c,0x7c,0x00,0x00,0x00,0x00,0x00,
/* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
#undef FONTDATAMAX

View File

@ -0,0 +1,19 @@
// Jakob Falke, oostubs
// Github: https://gitlab.cs.fau.de/um15ebek/oostubs
// vim: set et ts=4 sw=4:
#include "Fonts.h"
#include "Font_8x16.h"
#include "Font_8x8.h"
#include "Font_acorn_8x8.h"
#include "Font_pearl_8x8.h"
#include "Font_sun_12x22.h"
#include "Font_sun_8x16.h"
const Font_8x16 std_font_8x16;
const Font_8x8 std_font_8x8;
const Font_acorn_8x8 acorn_font_8x8;
const Font_pearl_8x8 pearl_font_8x8;
const Font_sun_12x22 sun_font_12x22;
const Font_sun_8x16 sun_font_8x16;

View File

@ -0,0 +1,67 @@
// Jakob Falke, oostubs
// Github: https://gitlab.cs.fau.de/um15ebek/oostubs
// Schriften in Form von Rastergrafiken (separate Datein)
// Generiert mit cpi2fnt
// Keine Proportionalschriften
// Monochrome Speicherung: 1 Bit pro Pixel
// Je nach Breite wird auf Bytegrenzen aufgerundet:
// 8 Pixel -> 1 Byte; 12 Pixel -> 2 Byte
#ifndef FONTS_H__
#define FONTS_H__
#include "lib/util/Array.h"
class Font {
public:
virtual ~Font() = default;
virtual const unsigned char* getChar(int c) const = 0;
virtual unsigned int get_char_width() const = 0;
virtual unsigned int get_char_height() const = 0;
};
template<unsigned int width, unsigned int height, const unsigned char* data>
class FontInstance : public Font {
const unsigned int char_width;
const unsigned int char_height;
const unsigned int char_mem_size;
const unsigned char* font_data;
public:
FontInstance() : char_width(width), char_height(height), char_mem_size((((char_width + (8 >> 1)) / 8) * char_height)), font_data(data) {}
inline const unsigned char* getChar(int c) const override {
return &font_data[char_mem_size * c];
}
inline unsigned int get_char_width() const override {
return char_width;
}
inline unsigned int get_char_height() const override {
return char_height;
}
};
extern const unsigned char fontdata_8x16[];
extern const unsigned char fontdata_8x8[];
extern const unsigned char acorndata_8x8[];
extern const unsigned char fontdata_pearl_8x8[];
extern const unsigned char fontdata_sun_12x22[];
extern const unsigned char fontdata_sun_8x16[];
using Font_8x16 = FontInstance<8, 16, fontdata_8x16>;
using Font_8x8 = FontInstance<8, 8, fontdata_8x8>;
using Font_acorn_8x8 = FontInstance<8, 8, acorndata_8x8>;
using Font_pearl_8x8 = FontInstance<8, 8, fontdata_pearl_8x8>;
using Font_sun_12x22 = FontInstance<12, 22, fontdata_sun_12x22>;
using Font_sun_8x16 = FontInstance<8, 16, fontdata_sun_8x16>;
extern const Font_8x16 std_font_8x16;
extern const Font_8x8 std_font_8x8;
extern const Font_acorn_8x8 acorn_font_8x8;
extern const Font_pearl_8x8 pearl_font_8x8;
extern const Font_sun_12x22 sun_font_12x22;
extern const Font_sun_8x16 sun_font_8x16;
#endif

View File

@ -0,0 +1,291 @@
/*****************************************************************************
* *
* L F B G R A P H I C S *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Zeichenfunktionen fuer Grafikmodi, die auf einem *
* linearen Framebuffer basieren. Verwendet in VESA und *
* QemuVGA. *
* *
* Autor: Michael Schoettner, HHU, 19.9.2016 *
* Der Code fuer das Zeichnen der Linie ist von Alan Wolfe *
* https://blog.demofox.org/2015/01/17/bresenhams-drawing-algorithms *
*****************************************************************************/
#include "LFBgraphics.h"
/* Hilfsfunktionen */
void swap(unsigned int* a, unsigned int* b);
int abs(int a);
/*****************************************************************************
* Methode: LFBgraphics::drawMonoBitmap *
*---------------------------------------------------------------------------*
* Parameter: x,y Startpunkt ab dem Text ausgegeben wird. *
* width Breite in Pixel *
* height Hoehe in Pixel *
* bitmap Zeiger auf Pixel der monochromen Rastergrafik *
* col Farbe der Pixel *
* *
* Beschreibung: Gibt die gegebene monochrome Rastergrafik an der Position*
* x,y zeilenweise aus. (x,y) ist der linke obere Punkt; *
* ist in der bitmap eine '1', so wird ein Pixel mit der *
* Farbe col ausgegeben, ansonsten bei '0' nichts. *
* Diese Funktion basiert auf dem Format der Fonts, welche *
* mit cpi2fnt (AmigaOS) erzeugt wurden. Das Format erklaert*
* sich in den C-Dateien in fonts/ von selbst. *
*****************************************************************************/
inline void LFBgraphics::drawMonoBitmap(unsigned int x, unsigned int y,
unsigned int width, unsigned int height,
const unsigned char* bitmap, unsigned int color) const {
// Breite in Bytes
unsigned short width_byte = width / 8 + ((width % 8 != 0) ? 1 : 0);
for (unsigned int yoff = 0; yoff < height; ++yoff) {
unsigned int xpos = x;
unsigned int ypos = y + yoff;
for (unsigned int xb = 0; xb < width_byte; ++xb) {
for (int src = 7; src >= 0; --src) {
if ((1 << src) & *bitmap) {
drawPixel(xpos, ypos, color);
}
xpos++;
}
bitmap++;
}
}
}
/*****************************************************************************
* Methode: LFBgraphics::drawString *
*---------------------------------------------------------------------------*
* Parameter: fnt Schrift *
* x,y Startpunkt ab dem Text ausgegeben wird. *
* col Farbe des Textes *
* str Zeiger auf Zeichenkette *
* len Laenge der Zeichenkette *
* *
* Beschreibung: Gibt eine Zeichenkette mit gewaehlter Schrift an der *
* Position x,y aus. *
*****************************************************************************/
void LFBgraphics::drawString(const Font& fnt, unsigned int x, unsigned int y,
unsigned int col, const char* str, unsigned int len) const {
for (unsigned int i = 0; i < len; ++i) {
drawMonoBitmap(x, y, fnt.get_char_width(), fnt.get_char_height(), fnt.getChar(*(str + i)), col);
x += fnt.get_char_width();
}
}
/*****************************************************************************
* Methode: LFBgraphics::drawPixel *
*---------------------------------------------------------------------------*
* Parameter: x, y Koordinaten des Pixels *
* col Farbe *
* *
* Beschreibung: Zeichnen eines Pixels. *
*****************************************************************************/
void LFBgraphics::drawPixel(unsigned int x, unsigned int y, unsigned int col) const {
unsigned char* ptr = reinterpret_cast<unsigned char*>(lfb);
if (hfb == 0 || lfb == 0) {
return;
}
if (mode == BUFFER_INVISIBLE) {
ptr = reinterpret_cast<unsigned char*>(hfb);
}
// Pixel ausserhalb des sichtbaren Bereichs?
if (x < 0 || x >= xres || y < 0 || y > yres) {
return;
}
// Adresse des Pixels berechnen und Inhalt schreiben
switch (bpp) {
case 8:
ptr += (x + y * xres);
*ptr = col;
return;
case 15:
case 16:
ptr += (2 * x + 2 * y * xres);
*ptr = col;
return;
case 24:
ptr += (3 * x + 3 * y * xres);
*ptr = (col & 0xFF);
ptr++;
*ptr = ((col >> 8) & 0xFF);
ptr++;
*ptr = ((col >> 16) & 0xFF);
ptr;
return;
case 32:
ptr += (4 * x + 4 * y * xres);
*ptr = (col & 0xFF);
ptr++;
*ptr = ((col >> 8) & 0xFF);
ptr++;
*ptr = ((col >> 16) & 0xFF);
ptr;
return;
}
}
void LFBgraphics::drawStraightLine(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col) const {
// Don't set mode inside the drawing function to use them in animations
if (x1 == x2 && y2 > y1) {
// Vertical line
for (unsigned int i = y1; i <= y2; ++i) {
drawPixel(x1, i, col);
}
} else if (y1 == y2 && x2 > x1) {
// Horizontal line
for (unsigned int i = x1; i <= x2; ++i) {
drawPixel(i, y1, col);
}
} else {
// Not straight
}
}
// (x1, y1)---(x2, y1)
// | |
// (x1, y2)---(x2, y2)
void LFBgraphics::drawRectangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col) const {
drawStraightLine(x1, y1, x2, y1, col);
drawStraightLine(x2, y1, x2, y2, col);
drawStraightLine(x1, y2, x2, y2, col);
drawStraightLine(x1, y1, x1, y2, col);
}
void LFBgraphics::drawCircle(unsigned int x, unsigned int y, unsigned int rad, unsigned int col) const {
// TODO
}
void LFBgraphics::drawSprite(unsigned int width, unsigned int height, unsigned int bytes_pp, const unsigned char* pixel_data) const {
const unsigned char* ptr;
for (unsigned int x = 0; x < width; ++x) {
for (unsigned int y = 0; y < height; ++y) {
ptr = pixel_data + (x + y * width) * bytes_pp;
switch (bytes_pp) {
case 2:
// TODO: Never tested, probably doesn't work
drawPixel(x, y, RGB_24(*ptr & 0b11111000, ((*ptr & 0b111) << 3) | (*(ptr + 1) >> 5),
*(ptr + 1) & 0b11111)); // RGB 565
break;
case 3:
case 4:
// Alpha gets ignored anyway
drawPixel(x, y, RGB_24(*ptr, *(ptr + 1), *(ptr + 2)));
break;
}
}
}
}
/*****************************************************************************
* Methode: LFBgraphics::clear *
*---------------------------------------------------------------------------*
* Beschreibung: Bildschirm loeschen. *
*****************************************************************************/
void LFBgraphics::clear() const {
unsigned int* ptr = reinterpret_cast<unsigned int*>(lfb);
unsigned int i;
if (hfb == 0 || lfb == 0) {
return;
}
if (mode == 0) {
ptr = reinterpret_cast<unsigned int*>(hfb);
}
switch (bpp) {
case 8:
for (i = 0; i < ((xres / 4) * yres); i++) {
*(ptr++) = 0;
}
return;
case 15:
case 16:
for (i = 0; i < (2 * (xres / 4) * yres); i++) {
*(ptr++) = 0;
}
return;
case 24:
for (i = 0; i < (3 * (xres / 4) * yres); i++) {
*(ptr++) = 0;
}
return;
case 32:
for (i = 0; i < (4 * (xres / 4) * yres); i++) {
*(ptr++) = 0;
}
return;
}
}
/*****************************************************************************
* Methode: LFBgraphics::setDrawingBuff *
*---------------------------------------------------------------------------*
* Beschreibung: Stellt ein, ob in den sichtbaren Puffer gezeichnet wird. *
*****************************************************************************/
void LFBgraphics::setDrawingBuff(int v) {
mode = v;
}
/*****************************************************************************
* Methode: LFBgraphics::copyHiddenToVisible *
*---------------------------------------------------------------------------*
* Beschreibung: Kopiert den versteckten Puffer in den sichtbaren LFB. *
*****************************************************************************/
void LFBgraphics::copyHiddenToVisible() const {
unsigned int* sptr = reinterpret_cast<unsigned int*>(hfb);
unsigned int* dptr = reinterpret_cast<unsigned int*>(lfb);
unsigned int i;
if (hfb == 0 || lfb == 0) {
return;
}
switch (bpp) {
case 8:
for (i = 0; i < ((xres / 4) * yres); i++) {
*(dptr++) = *(sptr++);
}
return;
case 15:
case 16:
for (i = 0; i < (2 * (xres / 4) * yres); i++) {
*(dptr++) = *(sptr++);
}
return;
case 24:
for (i = 0; i < (3 * (xres / 4) * yres); i++) {
*(dptr++) = *(sptr++);
}
return;
case 32:
for (i = 0; i < (4 * (xres / 4) * yres); i++) {
*(dptr++) = *(sptr++);
}
return;
}
}
void swap(unsigned int* a, unsigned int* b) {
unsigned int h = *a;
*a = *b;
*b = h;
}
int abs(int a) {
if (a < 0) {
return -a;
}
return a;
}

View File

@ -0,0 +1,64 @@
/*****************************************************************************
* *
* L F B G R A P H I C S *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Zeichenfunktionen fuer Grafikmodi, die auf einem *
* linearen Framebuffer basieren. Verwendet in VESA und *
* QemuVGA. *
* *
* Autor: Michael Schoettner, HHU, 19.9.2016 *
* Der Code fuer das Zeichnen der Linie ist von Alan Wolfe *
* https://blog.demofox.org/2015/01/17/bresenhams-drawing-algorithms *
*****************************************************************************/
#ifndef LFBgraphics_include__
#define LFBgraphics_include__
#include "Fonts.h"
// Hilfsfunktionen um Farbwerte fuer einen Pixel zu erzeugen
constexpr unsigned int RGB_24(unsigned int r, unsigned int g, unsigned int b) {
return ((r << 16) + (g << 8) + b);
}
constexpr const bool BUFFER_INVISIBLE = false;
constexpr const bool BUFFER_VISIBLE = true;
class LFBgraphics {
private:
// Hilfsfunktion fuer drawString
void drawMonoBitmap(unsigned int x, unsigned int y,
unsigned int width, unsigned int height,
const unsigned char* bitmap, unsigned int col) const;
public:
LFBgraphics(const LFBgraphics& copy) = delete; // Verhindere Kopieren
LFBgraphics() : mode(BUFFER_VISIBLE) {};
unsigned int xres, yres; // Aufloesung in Pixel
unsigned int bpp; // Farbtiefe (Bits per Pixel)
unsigned int lfb; // Adresse des Linearen Framebuffers
unsigned int hfb; // Adresse des versteckten Buffers (optional, fuer Animationen)
unsigned int mode; // Zeichnen im sichtbaren = 1 oder unsichtbaren = 0 Puffer
void clear() const;
void drawPixel(unsigned int x, unsigned int y, unsigned int col) const;
void drawString(const Font& fnt, unsigned int x, unsigned int y, unsigned int col, const char* str, unsigned int len) const;
void drawCircle(unsigned int x, unsigned int y, unsigned int rad, unsigned int col) const;
void drawStraightLine(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col) const;
void drawRectangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col) const;
void drawSprite(unsigned int width, unsigned int height, unsigned int bytes_pp, const unsigned char* pixel_data) const;
// stellt ein, ob in den sichtbaren Puffer gezeichnet wird
void setDrawingBuff(int v);
// kopiert 'hfb' nach 'lfb'
void copyHiddenToVisible() const;
};
#endif

131
src/device/graphics/VESA.cc Normal file
View File

@ -0,0 +1,131 @@
/*****************************************************************************
* *
* V E S A *
* *
*---------------------------------------------------------------------------*
* Beschreibung: VESA-Treiber ueber 16-Bit BIOS. *
* *
* Autor: Michael Schoettner, HHU, 18.3.2017 *
*****************************************************************************/
#include "VESA.h"
#include "device/bios/BIOS.h"
// Informationen ueber einen VESA-Grafikmodus
// (siehe http://wiki.osdev.org/VESA_Video_Modes)
struct VbeModeInfoBlock {
unsigned short attributes;
unsigned char winA, winB;
unsigned short granularity;
unsigned short winsize;
unsigned short segmentA, segmentB;
unsigned short realFctPtr[2];
unsigned short pitch; // Bytes pro Scanline
unsigned short Xres, Yres;
unsigned char Wchar, Ychar, planes, bpp, banks;
unsigned char memory_model, bank_size, image_pages;
unsigned char reserved0;
unsigned char red_mask, red_position;
unsigned char green_mask, green_position;
unsigned char blue_mask, blue_position;
unsigned char rsv_mask, rsv_position;
unsigned char directcolor_attributes;
unsigned int physbase; // Adresse des Linear-Framebuffers
unsigned int OffScreenMemOffset;
unsigned short OffScreenMemSize;
} __attribute__((packed));
// Informationen ueber die Grafikkarte
// (siehe http://wiki.osdev.org/VESA_Video_Modes)
struct VbeInfoBlock {
char VbeSignature[4]; // == "VESA"
unsigned short VbeVersion; // == 0x0300 for VBE 3.0
unsigned short OemStringPtr[2]; // isa vbeFarPtr
unsigned char Capabilities[4];
unsigned short VideoModePtr[2]; // isa vbeFarPtr
unsigned short TotalMemory; // as # of 64KB blocks
} __attribute__((packed));
/*****************************************************************************
* Methode: VESA::initTextMode *
*---------------------------------------------------------------------------*
* Beschreibung: Schalter in den Text-Modus 80x25 Zeichen. *
*****************************************************************************/
void VESA::initTextMode() {
BC_params->AX = 0x4f02; // SVFA BIOS, init mode
BC_params->BX = 0x4003; // 80x25
BIOS::Int(0x10);
}
/*****************************************************************************
* Methode: VESA::initGraphicMode *
*---------------------------------------------------------------------------*
* Parameter: Nummer des Grafikmodus (siehe VESA.h) *
* *
* Beschreibung: Bestimmten Grafikmodus einschalten. Dies wird durch *
* einen Aufruf des BIOS gemacht. *
*****************************************************************************/
bool VESA::initGraphicMode(unsigned short mode) {
// Alle Grafikmodi abfragen
BC_params->AX = 0x4F00;
BC_params->ES = RETURN_MEM >> 4;
BC_params->DI = RETURN_MEM & 0xF;
BIOS::Int(0x10);
VbeInfoBlock* ib = reinterpret_cast<VbeInfoBlock*>(RETURN_MEM);
// Signaturen pruefen
if (BC_params->AX != 0x004F) {
log.error() << "VESA wird nicht unterstuetzt." << endl;
return false;
}
if (ib->VbeSignature[0] != 'V' || ib->VbeSignature[1] != 'E' ||
ib->VbeSignature[2] != 'S' || ib->VbeSignature[3] != 'A') {
log.error() << "VESA wird nicht unterstuetzt." << endl;
return false;
}
// kout << "TotalVideoMemory: " << ((ib->TotalMemory*65536) / (1024*1024)) << " MB" << endl;
// Gewuenschten Grafikmodus aus Antwort suchen
unsigned short* modePtr = reinterpret_cast<unsigned short*>((ib->VideoModePtr[1] << 4) + ib->VideoModePtr[0]);
for (int i = 0; modePtr[i] != 0xFFFF; ++i) {
// Gewuenschter Grafikmodus gefunden?
if (modePtr[i] == mode) {
VbeModeInfoBlock* minf = reinterpret_cast<VbeModeInfoBlock*>(RETURN_MEM);
// Weitere Infos ueber diesen Grafikmodus abfragen
BC_params->AX = 0x4F01;
BC_params->CX = mode;
BC_params->ES = RETURN_MEM >> 4;
BC_params->DI = RETURN_MEM & 0xF;
BIOS::Int(0x10);
// Text-Modi 0-3 haben keinen LFB
if (mode > 3 && (minf->attributes & 0x90) == 0) {
log.error() << "Grafikmodus bietet keinen linearen Framebuffer." << endl;
return false;
}
mode_nr = mode;
xres = minf->Xres;
yres = minf->Yres;
bpp = static_cast<int>(minf->bpp);
lfb = minf->physbase;
hfb = reinterpret_cast<unsigned int>(new char[xres * yres * bpp / 8]);
// Grafikmodus einschalten
BC_params->AX = 0x4f02; // SVFA BIOS, init mode
BC_params->BX = mode;
BIOS::Int(0x10);
return true;
}
}
log.error() << "Grafikmodus nicht gefunden." << endl;
return false;
}

View File

@ -0,0 +1,42 @@
/*****************************************************************************
* *
* V E S A *
* *
*---------------------------------------------------------------------------*
* Beschreibung: VESA-Treiber ueber 16-Bit BIOS. *
* *
* Autor: Michael Schoettner, HHU, 19.5.2022 *
*****************************************************************************/
#ifndef VESA_include__
#define VESA_include__
#include "LFBgraphics.h"
#include "kernel/log/Logger.h"
// Ausgewaehlte Grafikmodi mit Mode-Nummer
constexpr const unsigned int MODE_640_480_16BITS = 0x111;
constexpr const unsigned int MODE_640_480_24BITS = 0x112;
constexpr const unsigned int MODE_800_600_16BITS = 0x114;
constexpr const unsigned int MODE_800_600_24BITS = 0x115;
constexpr const unsigned int MODE_1024_768_16BITS = 0x117;
constexpr const unsigned int MODE_1024_768_24BITS = 0x118;
class VESA : public LFBgraphics {
private:
int mode_nr; // Nummer des Modus
NamedLogger log;
public:
VESA(const VESA& copy) = delete; // Verhindere Kopieren
VESA() : log("VESA") {}
// Can't make singleton because atexit
// Bestimmten Grafikmodus einschalten
bool initGraphicMode(unsigned short mode);
static void initTextMode();
};
#endif

117
src/device/hid/Key.h Executable file
View File

@ -0,0 +1,117 @@
/*****************************************************************************
* *
* K E Y *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Taste, bestehend aus ASCII-, Scan-Code und Modifier-Bits.*
* *
* Autor: Olaf Spinczyk, TU Dortmund *
*****************************************************************************/
#ifndef Key_include__
#define Key_include__
class Key {
// Kopieren erlaubt!
unsigned char asc; // ASCII code
unsigned char scan; // scan code
unsigned char modi; // modifier
// Bit-Masken fuer die Modifier-Tasten
struct mbit {
enum {
shift = 1,
alt_left = 2,
alt_right = 4,
ctrl_left = 8,
ctrl_right = 16,
caps_lock = 32,
num_lock = 64,
scroll_lock = 128
};
};
public:
// DEFAULT-KONSTRUKTOR: setzt ASCII, Scancode und Modifier auf 0
// und bezeichnet so einen ungueltigen Tastencode
Key() : asc(0), scan(0), modi(0) {}
// VALID: mit Scancode = 0 werden ungueltige Tasten gekennzeichnet.
bool valid() const { return scan != 0; }
// INVALIDATE: setzt den Scancode auf Null und sorgt somit fuer einen
// ungueltigen Tastencode.
void invalidate() { scan = 0; }
// ASCII, SCANCODE: Setzen und Abfragen von Ascii und Scancode
void ascii(unsigned char a) { asc = a; }
void scancode(unsigned char s) { scan = s; }
unsigned char ascii() const { return asc; }
unsigned char scancode() const { return scan; }
//
// Funktionen zum Setzen und Loeschen von SHIFT, ALT, CTRL usw.
//
void shift(bool pressed) {
modi = pressed ? modi | mbit::shift : modi & ~mbit::shift;
}
void alt_left(bool pressed) {
modi = pressed ? modi | mbit::alt_left : modi & ~mbit::alt_left;
}
void alt_right(bool pressed) {
modi = pressed ? modi | mbit::alt_right : modi & ~mbit::alt_right;
}
void ctrl_left(bool pressed) {
modi = pressed ? modi | mbit::ctrl_left : modi & ~mbit::ctrl_left;
}
void ctrl_right(bool pressed) {
modi = pressed ? modi | mbit::ctrl_right : modi & ~mbit::ctrl_right;
}
void caps_lock(bool pressed) {
modi = pressed ? modi | mbit::caps_lock : modi & ~mbit::caps_lock;
}
void num_lock(bool pressed) {
modi = pressed ? modi | mbit::num_lock : modi & ~mbit::num_lock;
}
void scroll_lock(bool pressed) {
modi = pressed ? modi | mbit::scroll_lock : modi & ~mbit::scroll_lock;
}
//
// Funktionen zum Abfragen von SHIFT, ALT, CTRL usw.
//
bool shift() const { return (modi & mbit::shift) != 0; }
bool alt_left() const { return (modi & mbit::alt_left) != 0; }
bool alt_right() const { return (modi & mbit::alt_right) != 0; }
bool ctrl_left() const { return (modi & mbit::ctrl_left) != 0; }
bool ctrl_right() const { return (modi & mbit::ctrl_right) != 0; }
bool caps_lock() const { return (modi & mbit::caps_lock) != 0; }
bool num_lock() const { return (modi & mbit::num_lock) != 0; }
bool scroll_lock() const { return (modi & mbit::scroll_lock) != 0; }
bool alt() const { return alt_left() || alt_right(); }
bool ctrl() const { return ctrl_left() || ctrl_right(); }
operator char() const { return static_cast<char>(asc); }
// Scan-Codes einiger spezieller Tasten
struct scan {
enum {
f1 = 0x3b,
del = 0x53,
up = 72,
down = 80,
left = 75,
right = 77,
div = 8
};
};
};
#endif

349
src/device/hid/Keyboard.cc Executable file
View File

@ -0,0 +1,349 @@
/*****************************************************************************
* *
* K E Y B O A R D *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Treiber für den Tastaturcontroller des PCs. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
*****************************************************************************/
#include "Keyboard.h"
#include "kernel/system/Globals.h"
#include "Key.h"
const IOport Keyboard::ctrl_port(0x64);
const IOport Keyboard::data_port(0x60);
/* Tabellen fuer ASCII-Codes (Klassenvariablen) intiialisieren */
constexpr const unsigned char Keyboard::normal_tab[] = {
0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 225, 39, '\b',
0, 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', 129, '+', '\n',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 148, 132, '^', 0, '#',
'y', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '-', 0,
'*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-',
0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, '<', 0, 0};
constexpr const unsigned char Keyboard::shift_tab[] = {
0, 0, '!', '"', 21, '$', '%', '&', '/', '(', ')', '=', '?', 96, 0,
0, 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', 'O', 'P', 154, '*', 0,
0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 153, 142, 248, 0, 39,
'Y', 'X', 'C', 'V', 'B', 'N', 'M', ';', ':', '_', 0,
0, 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '>', 0, 0};
constexpr const unsigned char Keyboard::alt_tab[] = {
0, 0, 0, 253, 0, 0, 0, 0, '{', '[', ']', '}', '\\', 0, 0,
0, '@', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '~', 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 230, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '|', 0, 0};
constexpr const unsigned char Keyboard::asc_num_tab[] = {
'7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', ','};
constexpr const unsigned char Keyboard::scan_num_tab[] = {
8, 9, 10, 53, 5, 6, 7, 27, 2, 3, 4, 11, 51};
/*****************************************************************************
* Methode: Keyboard::key_decoded *
*---------------------------------------------------------------------------*
* Beschreibung: Interpretiert die Make- und Break-Codes der Tastatur. *
* *
* Rueckgabewert: true bedeutet, dass das Zeichen komplett ist *
* false es fehlen noch Make- oder Break-Codes. *
*****************************************************************************/
bool Keyboard::key_decoded() {
bool done = false;
// Die Tasten, die bei der MF II Tastatur gegenueber der aelteren
// AT Tastatur hinzugekommen sind, senden immer erst eines von zwei
// moeglichen Prefix Bytes.
if (code == prefix1 || code == prefix2) {
prefix = code;
return false;
}
// Das Loslassen einer Taste ist eigentlich nur bei den "Modifier" Tasten
// SHIFT, CTRL und ALT von Interesse, bei den anderen kann der Break-Code
// ignoriert werden.
if (code & break_bit) {
code &= ~break_bit; // Der Break-Code einer Taste ist gleich dem
// Make-Code mit gesetzten break_bit.
switch (code) {
case 42:
case 54:
gather.shift(false);
break;
case 56:
if (prefix == prefix1) {
gather.alt_right(false);
} else {
gather.alt_left(false);
}
break;
case 29:
if (prefix == prefix1) {
gather.ctrl_right(false);
} else {
gather.ctrl_left(false);
}
break;
}
// Ein Prefix gilt immer nur fuer den unmittelbar nachfolgenden Code.
// Also ist es jetzt abgehandelt.
prefix = 0;
// Mit einem Break-Code kann man nichts anfangen, also false liefern.
return false;
}
// Eine Taste wurde gedrueckt. Bei den Modifier Tasten wie SHIFT, ALT,
// NUM_LOCK etc. wird nur der interne Zustand geaendert. Durch den
// Rueckgabewert 'false' wird angezeigt, dass die Tastatureingabe noch
// nicht abgeschlossen ist. Bei den anderen Tasten werden ASCII
// und Scancode eingetragen und ein 'true' fuer eine erfolgreiche
// Tastaturabfrage zurueckgegeben, obwohl genaugenommen noch der Break-
// code der Taste fehlt.
switch (code) {
case 42:
case 54:
gather.shift(true);
break;
case 56:
if (prefix == prefix1) {
gather.alt_right(true);
} else {
gather.alt_left(true);
}
break;
case 29:
if (prefix == prefix1) {
gather.ctrl_right(true);
} else {
gather.ctrl_left(true);
}
break;
case 58:
gather.caps_lock(!gather.caps_lock());
set_led(led::caps_lock, gather.caps_lock());
break;
case 70:
gather.scroll_lock(!gather.scroll_lock());
set_led(led::scroll_lock, gather.scroll_lock());
break;
case 69: // Numlock oder Pause ?
if (gather.ctrl_left()) { // Pause Taste
// Auf alten Tastaturen konnte die Pause-Funktion wohl nur
// ueber Ctrl+NumLock erreicht werden. Moderne MF-II Tastaturen
// senden daher diese Codekombination, wenn Pause gemeint ist.
// Die Pause Taste liefert zwar normalerweise keinen ASCII-
// Code, aber Nachgucken schadet auch nicht. In jedem Fall ist
// die Taste nun komplett.
get_ascii_code();
done = true;
} else { // NumLock
gather.num_lock(!gather.num_lock());
set_led(led::num_lock, gather.num_lock());
}
break;
default: // alle anderen Tasten
// ASCII-Codes aus den entsprechenden Tabellen auslesen, fertig.
get_ascii_code();
done = true;
}
// Ein Prefix gilt immer nur fuer den unmittelbar nachfolgenden Code.
// Also ist es jetzt abgehandelt.
prefix = 0;
return done;
}
/*****************************************************************************
* Methode: Keyboard::get_ascii_code *
*---------------------------------------------------------------------------*
* Beschreibung: Ermittelt anhand von Tabellen aus dem Scancode und den *
* gesetzten Modifier-Bits den ASCII-Code der Taste. *
*****************************************************************************/
void Keyboard::get_ascii_code() {
// Sonderfall Scancode 53: Dieser Code wird sowohl von der Minustaste
// des normalen Tastaturbereichs, als auch von der Divisionstaste des
// Ziffernblocks gesendet. Damit in beiden Faellen ein Code heraus-
// kommt, der der Aufschrift entspricht, muss im Falle des Ziffern-
// blocks eine Umsetzung auf den richtigen Code der Divisionstaste
// erfolgen.
if (code == 53 && prefix == prefix1) { // Divisionstaste des Ziffernblocks
gather.ascii('/');
gather.scancode(Key::scan::div);
}
// Anhand der Modifierbits muss die richtige Tabelle ausgewaehlt
// werden. Der Einfachheit halber hat NumLock Vorrang vor Alt,
// Shift und CapsLock. Fuer Ctrl gibt es keine eigene Tabelle
else if (gather.num_lock() && !prefix && code >= 71 && code <= 83) {
// Bei eingeschaltetem NumLock und der Betaetigung einer der
// Tasten des separaten Ziffernblocks (Codes 71-83), sollen
// nicht die Scancodes der Cursortasten, sondern ASCII- und
// Scancodes der ensprechenden Zifferntasten geliefert werden.
// Die Tasten des Cursorblocks (prefix == prefix1) sollen
// natuerlich weiterhin zur Cursorsteuerung genutzt werden
// koennen. Sie senden dann uebrigens noch ein Shift, aber das
// sollte nicht weiter stoeren.
gather.ascii(asc_num_tab[code - 71]);
gather.scancode(scan_num_tab[code - 71]);
} else if (gather.alt_right()) {
gather.ascii(alt_tab[code]);
gather.scancode(code);
} else if (gather.shift()) {
gather.ascii(shift_tab[code]);
gather.scancode(code);
} else if (gather.caps_lock()) {
// Die Umschaltung soll nur bei Buchstaben gelten
if ((code >= 16 && code <= 26) || (code >= 30 && code <= 40) || (code >= 44 && code <= 50)) {
gather.ascii(shift_tab[code]);
gather.scancode(code);
} else {
gather.ascii(normal_tab[code]);
gather.scancode(code);
}
} else {
gather.ascii(normal_tab[code]);
gather.scancode(code);
}
}
/*****************************************************************************
* Konstruktor: Keyboard::Keyboard *
*---------------------------------------------------------------------------*
* Beschreibung: Initialisierung der Tastatur: alle LEDs werden ausge- *
* schaltet und die Wiederholungsrate auf maximale *
* Geschwindigkeit eingestellt. *
*****************************************************************************/
Keyboard::Keyboard() {
// alle LEDs ausschalten (bei vielen PCs ist NumLock nach dem Booten an)
set_led(led::caps_lock, false);
set_led(led::scroll_lock, false);
set_led(led::num_lock, false);
// maximale Geschwindigkeit, minimale Verzoegerung
set_repeat_rate(0, 0);
}
/*****************************************************************************
* Methode: Keyboard::key_hit *
*---------------------------------------------------------------------------*
* Beschreibung: Diese Methode soll einen Tastendruck zurueckliefern. *
* Hierzu soll die Tastatur in einer Schleife "gepollt" *
* werden, bis ein Zeichen eingegebn wurde. *
* *
* Das Byte von der Tastatur soll in dem Attribut 'code' *
* (siehe Keyboard.h) gespeichert werden. Die Dekodierung *
* soll mithilfe der vorgegebenen Funktion 'key_decoded' *
* erfolgen. *
* *
* Rückgabewert: Wenn der Tastendruck abgeschlossen ist und ein Scancode, *
* sowie gegebenenfalls ein ASCII-Code emittelt werden *
* konnte, werden diese in 'gather' (siehe Keyboard.h) *
* zurueckgeliefert. Anderenfalls liefert key_hit () einen *
* ungueltigen Wert zurueck, was mit Key::valid () *
* ueberprueft werden kann. *
*****************************************************************************/
Key Keyboard::key_hit() {
Key invalid; // nicht explizit initialisierte Tasten sind ungueltig
/* Hier muss Code eingefuegt werden. */
bool outbyte;
do {
outbyte = ctrl_port.inb() & outb;
} while (!outbyte);
// Ignore PS2 Mouse
bool auxbyte = ctrl_port.inb() & auxb;
if (auxbyte) {
return invalid;
}
code = data_port.inb();
if (key_decoded()) {
return gather;
}
return invalid;
}
/*****************************************************************************
* Methode: Keyboard::reboot *
*---------------------------------------------------------------------------*
* Beschreibung: Fuehrt einen Neustart des Rechners durch. *
*****************************************************************************/
void Keyboard::reboot() {
int status;
// Dem BIOS mitteilen, dass das Reset beabsichtigt war
// und kein Speichertest durchgefuehrt werden muss.
*reinterpret_cast<unsigned short*>(0x472) = 0x1234;
// Der Tastaturcontroller soll das Reset ausloesen.
do {
status = ctrl_port.inb(); // warten, bis das letzte Kommando
} while ((status & inpb) != 0); // verarbeitet wurde.
ctrl_port.outb(cpu_reset); // Reset
}
/*****************************************************************************
* Methode: Keyboard::set_repeat_rate *
*---------------------------------------------------------------------------*
* Beschreibung: Einstellen der Wiederholungsrate der Tastatur. *
* *
* Parameter: *
* delay: Bestimmt, wie lange eine Taste gedrueckt werden muss, *
* bevor die Wiederholung einsetzt. Erlaubt sind Werte *
* zw. 0 (minimale Wartezeit) und 3 (maximale Wartezeit). *
* speed: Bestimmt, wie schnell die Tastencodes aufeinander folgen *
* sollen. Erlaubt sind Werte zwischen 0 (sehr schnell) *
* und 31 (sehr langsam). *
*****************************************************************************/
void Keyboard::set_repeat_rate(int speed, int delay) {
/* Hier muss Code eingefuegt werden. */
}
/*****************************************************************************
* Methode: Keyboard::set_led *
*---------------------------------------------------------------------------*
* Beschreibung: Setzt oder loescht die angegebene Leuchtdiode. *
* *
* Parameter: *
* led: Welche LED? (caps_lock, num_lock, scroll_lock) *
* on: 0 = aus, 1 = an *
*****************************************************************************/
void Keyboard::set_led(char led, bool on) {
/* Hier muss Code eingefuegt werden. */
}
// Registriert die Tastatur ISR im IntDispatcher
// und erlaubt den keyboard interrupt im PIC
void Keyboard::plugin() {
intdis.assign(IntDispatcher::keyboard, *this);
PIC::allow(PIC::keyboard);
}
void Keyboard::trigger() {
Key key = key_hit();
// lastkey = key.ascii();
// NOTE: My keyboard has no delete key...
if (key.ctrl_left() && key.alt_left() && static_cast<char>(key) == 'r') {
reboot();
} else if (key != 0) {
kevman.broadcast(key); // Send key to all subscribed threads
}
}

100
src/device/hid/Keyboard.h Executable file
View File

@ -0,0 +1,100 @@
/*****************************************************************************
* *
* K E Y B O A R D *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Treiber für den Tastaturcontroller des PCs. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
* Modifikationen, Michael Schoettner, 2.6.2022 *
*****************************************************************************/
#ifndef Keyboard_include__
#define Keyboard_include__
#include "Key.h"
#include "kernel/interrupt/ISR.h"
#include "device/port/IOport.h"
class Keyboard : public ISR {
private:
unsigned char code; // Byte von Tastatur
unsigned char prefix; // Prefix von Tastatur
Key gather; // letzter dekodierter Key
char leds; // Zustand LEDs
// Benutzte Ports des Tastaturcontrollers
static const IOport ctrl_port; // Status- (R) u. Steuerregister (W)
static const IOport data_port; // Ausgabe- (R) u. Eingabepuffer (W)
// Bits im Statusregister
enum { outb = 0x01,
inpb = 0x02,
auxb = 0x20 };
// Kommandos an die Tastatur
struct kbd_cmd {
enum { set_led = 0xed,
set_speed = 0xf3 };
};
enum { cpu_reset = 0xfe };
// Namen der LEDs
struct led {
enum { caps_lock = 4,
num_lock = 2,
scroll_lock = 1 };
};
// Antworten der Tastatur
struct kbd_reply {
enum { ack = 0xfa };
};
// Konstanten fuer die Tastaturdekodierung
enum { break_bit = 0x80,
prefix1 = 0xe0,
prefix2 = 0xe1 };
// Klassenvariablen
static const unsigned char normal_tab[];
static const unsigned char shift_tab[];
static const unsigned char alt_tab[];
static const unsigned char asc_num_tab[];
static const unsigned char scan_num_tab[];
// Interpretiert die Make und Break-Codes der Tastatur.
bool key_decoded();
// Ermittelt anhand von Tabellen den ASCII-Code.
void get_ascii_code();
// Tastaturabfrage (vorerst Polling)
Key key_hit();
public:
Keyboard(const Keyboard& copy) = delete; // Verhindere Kopieren
// Initialisierung der Tastatur.
Keyboard();
// ~Keyboard() override = default;
// unsigned int lastkey; // speichert den ASCII-Code der zuletzt gedrückten Taste
// Fuehrt einen Neustart des Rechners durch.
static void reboot();
// Einstellen der Wiederholungsrate der Tastatur.
void set_repeat_rate(int speed, int delay);
// Setzt oder loescht die angegebene Leuchtdiode.
void set_led(char led, bool on);
// Aktivierung der Unterbrechungen fuer die Tastatur
void plugin();
// Unterbrechnungsroutine der Tastatur.
void trigger() override;
};
#endif

105
src/device/interrupt/PIC.cc Executable file
View File

@ -0,0 +1,105 @@
/*****************************************************************************
* *
* P I C *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Mit Hilfe des PICs koennen Hardware-Interrupts (IRQs) *
* einzeln zugelassen oder unterdrueckt werden. Auf diese *
* Weise wird also bestimmt, ob die Unterbrechung eines *
* Geraetes ueberhaupt an den Prozessor weitergegeben wird. *
* Selbst dann erfolgt eine Aktivierung der Unterbrechungs- *
* routine nur, wenn der Prozessor bereit ist, auf Unter- *
* brechungen zu reagieren. Dies kann mit Hilfe der Klasse *
* CPU festgelegt werden. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
*****************************************************************************/
#include "PIC.h"
IOport const PIC::IMR1(0x21); // interrupt mask register von PIC 1
IOport const PIC::IMR2(0xa1); // interrupt mask register von PIC 2
/*****************************************************************************
* Methode: PIC::allow *
*---------------------------------------------------------------------------*
* Beschreibung: Sorgt dafuer, dass der uebergebene IRQ ab sofort durch *
* den PIC an den Prozessor weitergereicht wird. Um eine *
* Unterbrechungsbehandlung zu ermoeglichen, muss *
* zusaetzlich CPU::enable_int() aufgerufen werden. *
* *
* Parameter: *
* irq: IRQ der erlaubt werden soll *
*****************************************************************************/
void PIC::allow(int irq) {
/* hier muss Code eingefuegt werden */
// NOTE: allow sets the bit to 0
unsigned char IMR;
unsigned char mask = ~(0x1 << (irq % 8));
if (irq < 8) {
// PIC 1
IMR = IMR1.inb(); // We don't want to change the other interrupt masks so use this as start value
IMR1.outb(IMR & mask);
} else {
// PIC 2
IMR = IMR2.inb();
IMR2.outb(IMR & mask);
}
}
/*****************************************************************************
* Methode: PIC::forbid *
*---------------------------------------------------------------------------*
* Beschreibung: Unterdrueckt mit Hilfe des PICs einen bestimmten IRQ. *
* *
* Parameter: *
* interrupt: IRQ der maskiert werden soll *
*****************************************************************************/
void PIC::forbid(int irq) {
/* hier muss Code eingefuegt werden */
// NOTE: forbid sets the bit to 1
unsigned char IMR;
unsigned char mask = 0x1 << (irq % 8);
if (irq < 8) {
// PIC 1
IMR = IMR1.inb(); // We don't want to change the other interrupt masks so use this as start value
IMR1.outb(IMR | mask);
} else {
// PIC 2
IMR = IMR2.inb();
IMR2.outb(IMR | mask);
}
}
/*****************************************************************************
* Methode: PIC::status *
*---------------------------------------------------------------------------*
* Beschreibung: Liefert den aktuellen Zustand des Maskierbits eines *
* bestimmten IRQs. *
* *
* Parameter: *
* irq: IRQ dessen Status erfragt werden soll *
*****************************************************************************/
bool PIC::status(int irq) {
/* hier muss Code eingefuegt werden */
unsigned char IMR;
if (irq < 8) {
// PIC 1
IMR = IMR1.inb();
} else {
// PIC 2
IMR = IMR2.inb();
}
// Use % 8 to account for two PICs
unsigned char mask = 0x1 << (irq % 8);
return IMR & mask;
}

51
src/device/interrupt/PIC.h Executable file
View File

@ -0,0 +1,51 @@
/*****************************************************************************
* *
* P I C *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Mit Hilfe des PICs koennen Hardware-Interrupts (IRQs) *
* einzeln zugelassen oder unterdrueckt werden. Auf diese *
* Weise wird also bestimmt, ob die Unterbrechung eines *
* Geraetes ueberhaupt an den Prozessor weitergegeben wird. *
* Selbst dann erfolgt eine Aktivierung der Unterbrechungs- *
* routine nur, wenn der Prozessor bereit ist, auf Unter- *
* brechungen zu reagieren. Dies kann mit Hilfe der Klasse *
* CPU festgelegt werden. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
*****************************************************************************/
#ifndef PIC_include__
#define PIC_include__
#include "device/port/IOport.h"
class PIC {
private:
static const IOport IMR1; // interrupt mask register von PIC 1
static const IOport IMR2; // interrupt mask register von PIC 2
public:
PIC(const PIC& copy) = delete; // Verhindere Kopieren
PIC() = default;
// Can't make static because atexit
// IRQ-Nummern von Geraeten
enum {
timer = 0, // Programmable Interrupt Timer (PIT)
keyboard = 1, // Tastatur
com1 = 4
};
// Freischalten der Weiterleitung eines IRQs durch den PIC an die CPU
static void allow(int irq);
// Unterdruecken der Weiterleitung eines IRQs durch den PIC an die CPU
static void forbid(int irq);
// Abfragen, ob die Weiterleitung fuer einen bestimmten IRQ unterdrueckt ist
static bool status(int interrupt_device);
};
#endif

97
src/device/port/IOport.h Executable file
View File

@ -0,0 +1,97 @@
/*****************************************************************************
* *
* I O P O R T *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Diese Klasse dient dem Zugriff auf die Ein-/Ausgabe *
* Ports des PCs. Beim PC gibt es einen gesonderten I/O- *
* Adressraum, der nur mittels der Maschineninstruktionen *
* 'in' und 'out' angesprochen werden kann. Ein IOport- *
* Objekt wird beim Erstellen an eine Adresse des I/O- *
* Adressraums gebunden und kann dann fuer byte- oder *
* wortweise Ein- oder Ausgaben verwendet werden. *
* *
* Autor: Michael Schoettner, 28.8.2016 *
*****************************************************************************/
#ifndef IOport_include__
#define IOport_include__
class IOport {
private:
// 16-Bit Adresse im I/O-Adressraum
const unsigned short address;
public:
// Konstruktor, speichert Port-Adresse
explicit IOport(unsigned short a) : address(a) {};
// Byteweise Ausgabe eines Wertes ueber einen I/O-Port.
void outb(unsigned char val) const {
asm volatile("outb %0, %1"
:
: "a"(val), "Nd"(address));
}
// NOTE: I added this for easier init of COM1 port
void outb(unsigned char offset, unsigned char val) const {
asm volatile("outb %0, %1"
:
: "a"(val), "Nd"(static_cast<unsigned short>(address + offset)));
}
// Wortweise Ausgabe eines Wertes ueber einen I/O-Port.
void outw(unsigned short val) const {
asm volatile("outw %0, %1"
:
: "a"(val), "Nd"(address));
}
// 32-Bit Ausgabe eines Wertes ueber einen I/O-Port.
void outdw(unsigned int val) const {
asm volatile("outl %0, %1"
:
: "a"(val), "Nd"(address));
}
// Byteweises Einlesen eines Wertes ueber einen I/O-Port.
unsigned char inb() const {
unsigned char ret;
asm volatile("inb %1, %0"
: "=a"(ret)
: "Nd"(address));
return ret;
}
// NOTE: I added this for COM1 port
unsigned char inb(unsigned char offset) const {
unsigned char ret;
asm volatile("inb %1, %0"
: "=a"(ret)
: "Nd"(static_cast<unsigned short>(address + offset)));
return ret;
}
// Wortweises Einlesen eines Wertes ueber einen I/O-Port.
unsigned short inw() const {
unsigned short ret;
asm volatile("inw %1, %0"
: "=a"(ret)
: "Nd"(address));
return ret;
}
// 32-Bit Einlesen eines Wertes ueber einen I/O-Port.
unsigned int indw() const {
unsigned int ret;
asm volatile("inl %1, %0"
: "=a"(ret)
: "Nd"(address));
return ret;
}
};
#endif

View File

@ -0,0 +1,47 @@
#include "SerialOut.h"
const IOport SerialOut::com1(0x3f8);
SerialOut::SerialOut() {
// NOTE: I could add different ports for every register but this was easier as it's that way on OSDev
com1.outb(1, 0x00); // Disable all interrupts
com1.outb(3, 0x80); // Enable DLAB (set baud rate divisor)
com1.outb(0x03); // Set divisor to 3 (lo byte) 38400 baud
com1.outb(1, 0x00); // (hi byte)
com1.outb(3, 0x03); // 8 bits, no parity, one stop bit
com1.outb(2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
com1.outb(4, 0x0B); // IRQs enabled, RTS/DSR set
com1.outb(4, 0x1E); // Set in loopback mode, test the serial chip
com1.outb(0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
// Check if serial is faulty (i.e: not same byte as sent)
if (com1.inb() == 0xAE) {
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
com1.outb(4, 0x0F);
}
}
int SerialOut::serial_received() {
return com1.inb(5) & 1;
}
int SerialOut::is_transmit_empty() {
return com1.inb(5) & 0x20;
}
char SerialOut::read() {
while (serial_received() == 0) {}
return com1.inb();
}
void SerialOut::write(const char a) {
while (is_transmit_empty() == 0) {}
com1.outb(a);
}
void SerialOut::write(const bse::string_view a) {
for (char current : a) {
write(current);
}
}

View File

@ -0,0 +1,28 @@
#ifndef SerialOut_Include_H_
#define SerialOut_Include_H_
#include "IOport.h"
#include "lib/util/String.h"
#include "lib/util/StringView.h"
// NOTE: I took this code from https://wiki.osdev.org/Serial_Ports
class SerialOut {
private:
static const IOport com1;
static int serial_received();
static int is_transmit_empty();
public:
SerialOut();
SerialOut(const SerialOut& copy) = delete;
// Can't make singleton because atexit
static char read();
static void write(char a);
static void write(const bse::string_view a);
};
#endif

845
src/device/sound/PCSPK.cc Executable file
View File

@ -0,0 +1,845 @@
/*****************************************************************************
* *
* P C S P K *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Mit Hilfe dieser Klasse kann man Toene auf dem *
* PC-Lautsprecher ausgeben. *
* *
* Achtung: Qemu muss mit dem Parameter -soundhw pcspk aufgerufen *
* werden. Ansonsten kann man nichts hoeren. *
* *
* Autor: Michael Schoettner, HHU, 22.9.2016 *
*****************************************************************************/
#include "PCSPK.h"
#include "kernel/system/Globals.h"
const IOport PCSPK::control(0x43);
const IOport PCSPK::data2(0x42);
const IOport PCSPK::ppi(0x61);
/*****************************************************************************
* Methode: PCSPK::play *
*---------------------------------------------------------------------------*
* Beschreibung: Ton abspielen. *
* *
* Rückgabewerte: f: Frequenz des Tons *
* len: Laenge des Tons in ms *
*****************************************************************************/
void PCSPK::play(float f, int len) {
int freq = static_cast<int>(f);
int cntStart = 1193180 / freq;
int status;
// Zaehler laden
control.outb(0xB6); // Zaehler-2 konfigurieren
data2.outb(cntStart % 256); // Zaehler-2 laden (Lobyte)
data2.outb(cntStart / 256); // Zaehler-2 laden (Hibyte)
// Lautsprecher einschalten
status = static_cast<int>(ppi.inb()); // Status-Register des PPI auslesen
ppi.outb(status | 3); // Lautpsrecher Einschalten
// Pause
delay(len);
// Lautsprecher ausschalten
off();
}
/*****************************************************************************
* Methode: PCSPK::off *
*---------------------------------------------------------------------------*
* Beschreibung: Lautsprecher ausschalten. *
*****************************************************************************/
void PCSPK::off() {
int status;
status = static_cast<int>(ppi.inb()); // Status-Register des PPI auslesen
ppi.outb((status >> 2) << 2); // Lautsprecher ausschalten
}
/*****************************************************************************
* Methode: PCSPK::delay *
*---------------------------------------------------------------------------*
* Beschreibung: Verzoegerung um X ms (in 1ms Schritten; Min. 1ms). *
* *
* Parameter: time (delay in ms) *
*****************************************************************************/
inline void PCSPK::delay(int time) {
/* Hier muess Code eingefuegt werden */
unsigned long start_time = systime;
// systime is incremented in 10ms steps
while ((systime - start_time) * 10 < time) {}
}
/*****************************************************************************
* Methode: PCSPK::tetris *
*---------------------------------------------------------------------------*
* Beschreibung: Tetris Sound, Kévin Rapaille, August 2013 *
* https://gist.github.com/XeeX/6220067 *
*****************************************************************************/
void PCSPK::tetris() {
play(658, 125);
play(1320, 500);
play(990, 250);
play(1056, 250);
play(1188, 250);
play(1320, 125);
play(1188, 125);
play(1056, 250);
play(990, 250);
play(880, 500);
play(880, 250);
play(1056, 250);
play(1320, 500);
play(1188, 250);
play(1056, 250);
play(990, 750);
play(1056, 250);
play(1188, 500);
play(1320, 500);
play(1056, 500);
play(880, 500);
play(880, 500);
delay(250);
play(1188, 500);
play(1408, 250);
play(1760, 500);
play(1584, 250);
play(1408, 250);
play(1320, 750);
play(1056, 250);
play(1320, 500);
play(1188, 250);
play(1056, 250);
play(990, 500);
play(990, 250);
play(1056, 250);
play(1188, 500);
play(1320, 500);
play(1056, 500);
play(880, 500);
play(880, 500);
delay(500);
play(1320, 500);
play(990, 250);
play(1056, 250);
play(1188, 250);
play(1320, 125);
play(1188, 125);
play(1056, 250);
play(990, 250);
play(880, 500);
play(880, 250);
play(1056, 250);
play(1320, 500);
play(1188, 250);
play(1056, 250);
play(990, 750);
play(1056, 250);
play(1188, 500);
play(1320, 500);
play(1056, 500);
play(880, 500);
play(880, 500);
delay(250);
play(1188, 500);
play(1408, 250);
play(1760, 500);
play(1584, 250);
play(1408, 250);
play(1320, 750);
play(1056, 250);
play(1320, 500);
play(1188, 250);
play(1056, 250);
play(990, 500);
play(990, 250);
play(1056, 250);
play(1188, 500);
play(1320, 500);
play(1056, 500);
play(880, 500);
play(880, 500);
delay(500);
play(660, 1000);
play(528, 1000);
play(594, 1000);
play(495, 1000);
play(528, 1000);
play(440, 1000);
play(419, 1000);
play(495, 1000);
play(660, 1000);
play(528, 1000);
play(594, 1000);
play(495, 1000);
play(528, 500);
play(660, 500);
play(880, 1000);
play(838, 2000);
play(660, 1000);
play(528, 1000);
play(594, 1000);
play(495, 1000);
play(528, 1000);
play(440, 1000);
play(419, 1000);
play(495, 1000);
play(660, 1000);
play(528, 1000);
play(594, 1000);
play(495, 1000);
play(528, 500);
play(660, 500);
play(880, 1000);
play(838, 2000);
off();
}
/*****************************************************************************
* Methode: PCSPK::tetris *
*---------------------------------------------------------------------------*
* Beschreibung: Clint, Part of Daft Punks Aerodynamic *
* https://www.kirrus.co.uk/2010/09/linux-beep-music/ *
*****************************************************************************/
void PCSPK::aerodynamic() {
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(370.0, 122);
play(493.9, 122);
play(370.0, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(587.3, 122);
play(415.3, 122);
play(493.9, 122);
play(415.3, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(784.0, 122);
play(493.9, 122);
play(659.3, 122);
play(493.9, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(440.0, 122);
play(659.3, 122);
play(440.0, 122);
play(554.4, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(740.0, 122);
play(987.8, 122);
play(740.0, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1174.7, 122);
play(830.6, 122);
play(987.8, 122);
play(830.6, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1568.0, 122);
play(987.8, 122);
play(1318.5, 122);
play(987.8, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
play(1318.5, 122);
play(880.0, 122);
play(1108.7, 122);
play(880.0, 122);
off();
}

89
src/device/sound/PCSPK.h Executable file
View File

@ -0,0 +1,89 @@
/*****************************************************************************
* *
* P C S P K *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Mit Hilfe dieser Klasse kann man Toene auf dem *
* PC-Lautsprecher ausgeben. *
* *
* Achtung: Qemu muss mit dem Parameter -soundhw pcspk aufgerufen *
* werden. Ansonsten kann man nichts hoeren. *
* *
* Autor: Michael Schoettner, HHU, 22.9.2016 *
*****************************************************************************/
#ifndef PCSPK_include__
#define PCSPK_include__
#include "device/port/IOport.h"
// Note, Frequenz
constexpr const float C0 = 130.81;
constexpr const float C0X = 138.59;
constexpr const float D0 = 146.83;
constexpr const float D0X = 155.56;
constexpr const float E0 = 164.81;
constexpr const float F0 = 174.61;
constexpr const float F0X = 185.00;
constexpr const float G0 = 196.00;
constexpr const float G0X = 207.65;
constexpr const float A0 = 220.00;
constexpr const float A0X = 233.08;
constexpr const float B0 = 246.94;
constexpr const float C1 = 261.63;
constexpr const float C1X = 277.18;
constexpr const float D1 = 293.66;
constexpr const float D1X = 311.13;
constexpr const float E1 = 329.63;
constexpr const float F1 = 349.23;
constexpr const float F1X = 369.99;
constexpr const float G1 = 391.00;
constexpr const float G1X = 415.30;
constexpr const float A1 = 440.00;
constexpr const float A1X = 466.16;
constexpr const float B1 = 493.88;
constexpr const float C2 = 523.25;
constexpr const float C2X = 554.37;
constexpr const float D2 = 587.33;
constexpr const float D2X = 622.25;
constexpr const float E2 = 659.26;
constexpr const float F2 = 698.46;
constexpr const float F2X = 739.99;
constexpr const float G2 = 783.99;
constexpr const float G2X = 830.61;
constexpr const float A2 = 880.00;
constexpr const float A2X = 923.33;
constexpr const float B2 = 987.77;
constexpr const float C3 = 1046.50;
class PCSPK {
private:
static const IOport control; // Steuerregister (write only)
static const IOport data2; // Zaehler-2 Datenregister
static const IOport ppi; // Status-Register des PPI
// Verzoegerung um X ms (in 1ms Schritten; Min. 1ms)
static inline void delay(int time);
public:
PCSPK(const PCSPK& copy) = delete; // Verhindere Kopieren
// Konstruktor. Initialisieren der Ports.
PCSPK() = default;
// Can't make singleton because atexit
// Demo Sounds
static void tetris();
static void aerodynamic();
// Ton abspielen
static void play(float f, int len);
// Lautsprecher ausschalten
static void off();
};
#endif

88
src/device/time/PIT.cc Executable file
View File

@ -0,0 +1,88 @@
/*****************************************************************************
* *
* P I T *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Programmable Interval Timer. *
* *
* Autor: Michael Schoettner, 3.7.2022 *
*****************************************************************************/
#include "PIT.h"
#include "kernel/system/Globals.h"
const IOport PIT::control(0x43);
const IOport PIT::data0(0x40);
/*****************************************************************************
* Methode: PIT::interval *
*---------------------------------------------------------------------------*
* Beschreibung: Zeitinervall programmieren. *
* *
* Parameter: *
* us: Zeitintervall in Mikrosekunden, nachdem periodisch ein *
* Interrupt erzeugt werden soll. *
*****************************************************************************/
void PIT::interval(int us) {
/* hier muss Code eingefuegt werden */
control.outb(0x36); // Zähler 0 Mode 3
unsigned int cntStart = static_cast<unsigned int>((1193180.0 / 1000000.0) * us); // 1.19Mhz PIT
data0.outb(cntStart & 0xFF); // Zaehler-0 laden (Lobyte)
data0.outb(cntStart >> 8); // Zaehler-0 laden (Hibyte)
}
/*****************************************************************************
* Methode: PIT::plugin *
*---------------------------------------------------------------------------*
* Beschreibung: Unterbrechungen fuer den Zeitgeber erlauben. Ab sofort *
* wird bei Ablauf des definierten Zeitintervalls die *
* Methode 'trigger' aufgerufen. *
*****************************************************************************/
void PIT::plugin() {
/* hier muss Code eingefuegt werden */
intdis.assign(IntDispatcher::timer, *this);
PIC::allow(PIC::timer);
}
/*****************************************************************************
* Methode: PIT::trigger *
*---------------------------------------------------------------------------*
* Beschreibung: ISR fuer den Zeitgeber. Wird aufgerufen, wenn der *
* Zeitgeber eine Unterbrechung ausloest. Anzeige der Uhr *
* aktualisieren und Thread wechseln durch Setzen der *
* Variable 'forceSwitch', wird in 'int_disp' behandelt. *
*****************************************************************************/
void PIT::trigger() {
/* hier muss Code eingefuegt werden */
// log << TRACE << "Incrementing systime" << endl;
// alle 10ms, Systemzeit weitersetzen
systime++;
// Bei jedem Tick einen Threadwechsel ausloesen.
// Aber nur wenn der Scheduler bereits fertig intialisiert wurde
// und ein weiterer Thread rechnen moechte
/* hier muss Code eingefuegt werden */
// Indicator
if (systime - last_indicator_refresh >= 10) {
indicator_pos = (indicator_pos + 1) % 4;
CGA::show(79, 0, indicator[indicator_pos]);
last_indicator_refresh = systime;
}
// Preemption
if (scheduler.preemption_enabled()) {
// log << TRACE << "Preemption" << endl;
scheduler.preempt();
}
}

54
src/device/time/PIT.h Executable file
View File

@ -0,0 +1,54 @@
/*****************************************************************************
* *
* P I T *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Programmable Interval Timer. *
* *
* Autor: Michael Schoettner, 23.8.2016 *
*****************************************************************************/
#ifndef PIT_include__
#define PIT_include__
#include "kernel/interrupt/ISR.h"
#include "device/port/IOport.h"
#include "lib/util/Array.h"
class PIT : public ISR {
private:
const static IOport control;
const static IOport data0;
enum { time_base = 838 }; /* ns */
int timer_interval;
const bse::array<char, 4> indicator{'|', '/', '-', '\\'};
unsigned int indicator_pos = 0;
unsigned long last_indicator_refresh = 0;
public:
PIT(const PIT& copy) = delete; // Verhindere Kopieren
// ~PIT() override = default;
// Zeitgeber initialisieren.
explicit PIT(int us) {
PIT::interval(us);
}
// Konfiguriertes Zeitintervall auslesen.
int interval() const { return timer_interval; }
// Zeitintervall in Mikrosekunden, nachdem periodisch ein Interrupt
//erzeugt werden soll.
static void interval(int us);
// Aktivierung der Unterbrechungen fuer den Zeitgeber
void plugin();
// Unterbrechnungsroutine des Zeitgebers.
void trigger() override;
};
#endif

View File

@ -0,0 +1,40 @@
#include "ArrayDemo.h"
void ArrayDemo::run() {
bse::array<int, 10> arr1 {};
bse::array<int, 10> arr2 {};
kout.lock();
kout.clear();
kout << "Adding..." << endl;
for (int i = 0; i < 10; ++i) {
arr1[i] = i;
}
kout << "Iterator printing arr1:" << endl;
for (int i : arr1) {
kout << i << " ";
}
kout << endl;
kout << "Swapping arr1 and arr2..." << endl;
arr1.swap(arr2);
kout << "Iterator printing arr1:" << endl;
for (int i : arr1) {
kout << i << " ";
}
kout << endl;
kout << "Iterator printing arr2:" << endl;
for (int i : arr2) {
kout << i << " ";
}
kout << endl;
// arr1.swap(arr3); // Not possible as type/size has to match
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,16 @@
#ifndef ArrayDemo_include__
#define ArrayDemo_include__
#include "kernel/system/Globals.h"
#include "lib/util/Array.h"
class ArrayDemo : public Thread {
public:
ArrayDemo(const ArrayDemo& copy) = delete;
ArrayDemo() : Thread("ArrayDemo") {}
void run() override;
};
#endif

78
src/kernel/demo/HeapDemo.cc Executable file
View File

@ -0,0 +1,78 @@
/*****************************************************************************
* *
* H E A P D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Demonstration der dynamischen Speicherverwaltung. *
* *
* Autor: Michael Schoettner, HHU, 27.12.2016 *
*****************************************************************************/
#include "HeapDemo.h"
void HeapDemo::run() {
kout.lock();
kout.clear();
kout << "HEAP_DEMO ===================================================================" << endl;
/* hier muss Code eingefuegt werden */
allocator.dump_free_memory();
// Rounding to word border
kout << "ROUNDING ====================================================================" << endl;
void* alloc = allocator.alloc(1); // 1 Byte
allocator.dump_free_memory();
allocator.free(alloc);
allocator.dump_free_memory();
// Some objects and forward/backward merging
kout << "SOME OBJECTS ================================================================" << endl;
MyObj* a = new MyObj(5);
allocator.dump_free_memory();
MyObj* b = new MyObj(10);
allocator.dump_free_memory();
MyObj* c = new MyObj(15);
allocator.dump_free_memory();
delete b; // No merge
allocator.dump_free_memory();
delete a; // Merge forward BUG: Bluescreen
allocator.dump_free_memory();
delete c;
allocator.dump_free_memory();
// Allocate too whole heap
// void* ptr = allocator.alloc(1024 * 1024 - 24);
// allocator.dump_free_memory();
// allocator.free(ptr);
// allocator.dump_free_memory();
// Allocate too much
kout << "TOO MUCH ====================================================================" << endl;
allocator.alloc(1024 * 1024); // should fail as only 1024 * 1024 - Headersize bytes are available
allocator.dump_free_memory();
// A lot of allocations
// MyObj* objs[1024];
// for (unsigned int i = 0; i < 1024; ++i) {
// objs[i] = new MyObj(5);
// }
// allocator.dump_free_memory();
// waitForReturn();
// for (unsigned int i = 0; i < 1024; ++i) {
// delete objs[i];
// }
// allocator.dump_free_memory();
// Array allocation
kout << "ARRAY =======================================================================" << endl;
MyObj* objs = new MyObj[1024];
allocator.dump_free_memory();
delete[] objs;
allocator.dump_free_memory();
kout << "HEAP_DEMO END ===============================================================" << endl;
kout.unlock();
scheduler.exit();
}

32
src/kernel/demo/HeapDemo.h Executable file
View File

@ -0,0 +1,32 @@
/*****************************************************************************
* *
* H E A P D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Demonstration der dynamischen Speicherverwaltung. *
* *
* Autor: Michael Schoettner, HHU, 25.9.2016 *
*****************************************************************************/
#ifndef HeapDemo_include__
#define HeapDemo_include__
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
class MyObj {
public:
constexpr MyObj() : value(5) {};
constexpr MyObj(const unsigned int val) : value(val) {};
const unsigned int value;
};
class HeapDemo : public Thread {
public:
HeapDemo(const HeapDemo& copy) = delete;
HeapDemo() : Thread("HeapDemo") {}
void run() override;
};
#endif

34
src/kernel/demo/KeyboardDemo.cc Executable file
View File

@ -0,0 +1,34 @@
/*****************************************************************************
* *
* K E Y B O A R D D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Testausgaben für den CGA-Treiber. *
* *
* Autor: Michael Schoettner, HHU, 26.10.2018 *
*****************************************************************************/
#include "KeyboardDemo.h"
void KeyboardDemo::run() {
/* Hier muess Code eingefuegt werden */
kout << "Keyboard Demo: " << endl;
kout.lock();
kout.clear();
kout << "Info: Die Keyboard Demo sperrt den Output Stream:\n"
<< " Wenn die Preemption Demo laeuft wird diese also erst\n"
<< " fortfahren wenn die Keyboard Demo wieder beendet ist." << endl;
kout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nInput: ";
kout.flush();
while (running) {
kout << listener.waitForKeyEvent();
kout.flush();
}
kout.unlock();
scheduler.exit();
}

44
src/kernel/demo/KeyboardDemo.h Executable file
View File

@ -0,0 +1,44 @@
/*****************************************************************************
* *
* K E Y B O A R D D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Testausgaben für den CGA-Treiber. *
* *
* Autor: Michael Schoettner, HHU, 26.10.2018 *
*****************************************************************************/
#ifndef KeyboardDemo_include__
#define KeyboardDemo_include__
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
#include "kernel/event/KeyEventListener.h"
class KeyboardDemo : public Thread {
private:
KeyEventListener listener;
public:
KeyboardDemo(const KeyboardDemo& copy) = delete;
KeyboardDemo() : Thread("KeyboardDemo"), listener(tid) {
kevman.subscribe(listener);
}
// Base class destructor will be called automatically
~KeyboardDemo() override {
if (running) {
// NOTE: If the thread was exited nicely it can unlock before destructor,
// but on forced kill kout has to be unlocked in the destructor.
// This is bad since it could release the lock although some other
// thread set it (so use nice_kill)
kout.unlock();
}
kevman.unsubscribe(listener);
}
void run() override;
};
#endif

View File

@ -0,0 +1,90 @@
#include "MainMenu.h"
#include "ArrayDemo.h"
#include "HeapDemo.h"
#include "KeyboardDemo.h"
#include "PagingDemo.h"
#include "PCSPKdemo.h"
#include "PreemptiveThreadDemo.h"
#include "SmartPointerDemo.h"
#include "StringDemo.h"
#include "TextDemo.h"
#include "VBEdemo.h"
#include "VectorDemo.h"
void print_demo_menu() {
kout.lock();
kout.clear();
kout << "Demo Menu, press number to start, k/K to kill:\n"
<< "1 - Text Demo\n"
<< "2 - PCSPK Demo\n"
<< "3 - Keyboard Demo\n"
<< "4 - Heap Demo\n"
<< "5 - VBE Demo\n"
<< "6 - Paging Demo\n"
<< "7 - Preemption Demo\n"
<< "Extra demos:\n"
<< "8 - bse::vector demo\n"
<< "9 - bse::array demo\n"
<< "0 - bse::unique_ptr demo\n"
<< "! - bse::string demo\n"
<< endl;
kout.unlock();
}
void MainMenu::run() {
print_demo_menu();
char input = '\0';
unsigned int running_demo = 0;
while (running) {
input = listener.waitForKeyEvent();
if ((input >= '0' && input <= '9') || input == '!') {
switch (input) {
case '1':
running_demo = scheduler.ready<TextDemo>();
break;
case '2':
running_demo = scheduler.ready<PCSPKdemo>(&PCSPK::aerodynamic);
break;
case '3':
running_demo = scheduler.ready<KeyboardDemo>();
break;
case '4':
running_demo = scheduler.ready<HeapDemo>();
break;
case '5':
running_demo = scheduler.ready<VBEdemo>();
break;
case '6':
running_demo = scheduler.ready<PagingDemo>();
break;
case '7':
running_demo = scheduler.ready<PreemptiveThreadDemo>(3);
break;
case '8':
running_demo = scheduler.ready<VectorDemo>();
break;
case '9':
running_demo = scheduler.ready<ArrayDemo>();
break;
case '0':
running_demo = scheduler.ready<SmartPointerDemo>();
break;
case '!':
running_demo = scheduler.ready<StringDemo>();
break;
}
} else if (input == 'k') {
scheduler.nice_kill(running_demo); // NOTE: If thread exits itself this will throw error
print_demo_menu();
} else if (input == 'K') {
scheduler.kill(running_demo);
print_demo_menu();
}
}
scheduler.exit();
// This thread won't be deleted...
}

View File

@ -0,0 +1,26 @@
#ifndef MainMenu_Inlucde_H_
#define MainMenu_Inlucde_H_
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
#include "kernel/event/KeyEventListener.h"
class MainMenu : public Thread {
private:
KeyEventListener listener;
public:
MainMenu(const MainMenu& copy) = delete;
MainMenu() : Thread("MainMenu"), listener(tid) {
kevman.subscribe(listener);
}
~MainMenu() override {
kevman.unsubscribe(listener);
}
void run() override;
};
#endif

View File

@ -0,0 +1,16 @@
#include "PCSPKdemo.h"
void PCSPKdemo::run() {
kout.lock();
kout.clear();
kout << "Playing..." << endl;
kout.unlock();
(*melody)(); // This syntax is confusing as hell
kout.lock();
kout << "Finished" << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,23 @@
#ifndef PCSPKdemo_INCLUDE_H_
#define PCSPKdemo_INCLUDE_H_
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
class PCSPKdemo : public Thread {
private:
void (*melody)(); // Allow to pass a melody to play when initializing the demo
public:
PCSPKdemo(const PCSPKdemo& copy) = delete;
PCSPKdemo(void (*melody)()) : Thread("PCSPKdemo"), melody(melody) {}
~PCSPKdemo() override {
PCSPK::off();
}
void run() override;
};
#endif

View File

@ -0,0 +1,50 @@
#include "PagingDemo.h"
void PagingDemo::writeprotect_page() {
kout << "Accessing a writeprotected page triggers bluescreen,\nif you can read this it didn't work" << endl;
// BlueScreen 1
// asm("int $3");
// BlueScreen 2
log.info() << "Allocating page" << endl;
unsigned int* page = pg_alloc_page();
*page = 42;
log.info() << "Writeprotecting page..." << endl;
pg_write_protect_page(page);
log.info() << "Accessing writeprotected page" << endl;
*page = 42; // We map logical to physical 1:1 so no need to do any lookup
// No free because bluescreen
}
void PagingDemo::notpresent_page() {
kout << "Produces pagefault, if you can read this it didn't work" << endl;
log.info() << "Allocating page" << endl;
unsigned int* page = pg_alloc_page();
*page = 42;
log.info() << "Marking page notpresent..." << endl;
pg_notpresent_page(page);
log.info() << "Accessing page" << endl;
kout << "Page not accessible: " << *page << endl;
// No free because bluescreen
}
void PagingDemo::run() {
kout << "Press w for writeprotect demo, n for notpresent demo" << endl;
switch(listener.waitForKeyEvent()) {
case 'w':
writeprotect_page();
break;
case 'n':
notpresent_page();
break;
}
scheduler.exit();
}

View File

@ -0,0 +1,30 @@
#ifndef BlueScreenDemo_include__
#define BlueScreenDemo_include__
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
#include "kernel/event/KeyEventListener.h"
class PagingDemo: public Thread {
private:
void writeprotect_page();
void free_page();
void notpresent_page();
KeyEventListener listener;
public:
PagingDemo(const PagingDemo& copy) = delete;
PagingDemo(): Thread("PagingDemo"), listener(tid) {
kevman.subscribe(listener);
}
~PagingDemo() override {
kevman.unsubscribe(listener);
}
void run() override;
};
#endif

View File

@ -0,0 +1,34 @@
#include "PreemptiveThreadDemo.h"
void PreemptiveLoopThread::run() {
int cnt = 0;
while (running) {
// Basic synchronization by semaphore
kout.lock();
// Saving + restoring kout position doesn't help much as preemption still occurs
CGA_Stream::setpos(55, id);
kout << fillw(3) << id << fillw(0) << ": " << dec << cnt++ << endl;
kout.unlock();
}
scheduler.exit();
}
void PreemptiveThreadDemo::run() {
kout.lock();
kout.clear();
kout << "Preemptive Thread Demo:" << endl;
kout << "Readying LoopThreads" << endl;
for (unsigned int i = 0; i < number_of_threads; ++i) {
scheduler.ready<PreemptiveLoopThread>(i);
}
kout << "Exiting main thread" << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,35 @@
#ifndef preemptive_thread_include__
#define preemptive_thread_include__
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
#include "lib/async/Semaphore.h"
class PreemptiveLoopThread : public Thread {
private:
int id;
public:
PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren
// Gibt der Loop einen Stack und eine Id.
PreemptiveLoopThread(int i) : Thread("LoopThread"), id(i) {}
// Zaehlt einen Zaehler hoch und gibt ihn auf dem Bildschirm aus.
void run() override;
};
class PreemptiveThreadDemo : public Thread {
private:
unsigned int number_of_threads;
public:
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
PreemptiveThreadDemo(unsigned int n) : Thread("PreemptiveThreadDemo"), number_of_threads(n) {}
// Thread-Startmethode
void run() override;
};
#endif

View File

@ -0,0 +1,122 @@
#include "SmartPointerDemo.h"
#include "kernel/process/IdleThread.h"
void SmartPointerDemo::run() {
kout.lock();
kout.clear();
kout << "Output is written to log to be able to trace memory allocations/deallocations" << endl;
{
log.info() << "Allocating new unique_ptr<int>..." << endl;
bse::unique_ptr<int> ptr = bse::make_unique<int>(1);
log.info() << "Leaving scope..." << endl;
}
log.info() << "Should be deleted by now..." << endl;
{
log.info() << "Allocating new unique_ptr<int>..." << endl;
bse::unique_ptr<int> ptr1 = bse::make_unique<int>(1);
bse::unique_ptr<int> ptr2;
log.info() << "*ptr1 == " << *ptr1 << ", (bool)ptr2 == " << static_cast<bool>(ptr2) << endl;
log.info() << "Moving ptr1 => ptr2 (no allocations should happen)..." << endl;
ptr2 = std::move(ptr1);
log.info() << "(bool)ptr1 == " << static_cast<bool>(ptr1) << ", *ptr2 == " << *ptr2 << endl;
log.info() << "Leaving scope..." << endl;
}
log.info() << "Should be deleted by now..." << endl;
{
log.info() << "Allocating (2) new unique_ptr<int>..." << endl;
bse::unique_ptr<int> ptr1 = bse::make_unique<int>(1);
bse::unique_ptr<int> ptr2 = bse::make_unique<int>(1);
log.info() << "Moving ptr1 => ptr2 (ptr1 should be freed)..." << endl;
ptr2 = std::move(ptr1);
log.info() << "Leaving scope..." << endl;
}
log.info() << "Should be deleted by now..." << endl;
// =====================================================================
{
log.info() << "Allocating new unique_ptr<int[]>..." << endl;
bse::unique_ptr<int[]> ptr = bse::make_unique<int[]>(10);
ptr[0] = 1;
log.info() << "ptr[0] == " << ptr[0] << endl;
}
log.info() << "Should be deleted by now..." << endl;
{
log.info() << "Allocating new unique_ptr<int[10]>..." << endl;
bse::unique_ptr<int[]> ptr1 = bse::make_unique<int[]>(10);
bse::unique_ptr<int[]> ptr2;
log.info() << "Moving ptr1 => ptr2 (no allocations should happen)..." << endl;
ptr2 = std::move(ptr1);
log.info() << "Leaving scope..." << endl;
}
log.info() << "Should be deleted by now..." << endl;
{
log.info() << "Allocating (2) new unique_ptr<int[10]>..." << endl;
bse::unique_ptr<int> ptr1 = bse::make_unique<int>(10);
bse::unique_ptr<int> ptr2 = bse::make_unique<int>(10);
log.info() << "Moving ptr1 => ptr2 (ptr1 should be freed)..." << endl;
ptr2 = std::move(ptr1);
log.info() << "Leaving scope..." << endl;
}
log.info() << "Should be deleted by now..." << endl;
// NOTE: This wasn't working because of a missing operator[] delete in the allocator
log.info() << "Allocating unique_ptr<int>*..." << endl;
bse::unique_ptr<int>* ptrptr = new bse::unique_ptr<int>[10];
delete[] ptrptr;
log.info() << "Should be deleted by now..." << endl;
// =====================================================================
{
log.info() << "Stackallocating Array<bse::unique_ptr<int>, 10>..." << endl;
bse::array<bse::unique_ptr<int>, 10> arr;
log.info() << "Populating slot 0..." << endl;
arr[0] = bse::make_unique<int>(1);
log.info() << "Moving slot 0 to slot 1..." << endl;
arr[1] = std::move(arr[0]);
log.info() << "Leaving scope" << endl;
}
log.info() << "Should be deleted by now..." << endl;
{
log.info() << "Heapallocating Array<bse::unique_ptr<int>, 10>..." << endl;
bse::array<bse::unique_ptr<int>, 10>* arr = new bse::array<bse::unique_ptr<int>, 10>;
log.info() << "Populating slot 0..." << endl;
(*arr)[0] = bse::make_unique<int>(1);
log.info() << "Moving slot 0 to slot 1..." << endl;
(*arr)[1] = std::move((*arr)[0]);
log.info() << "Deleting" << endl;
delete arr;
log.info() << "Leaving scope" << endl;
}
log.info() << "Should be deleted by now..." << endl;
{
log.info() << "ArrayList<bse::unique_ptr<int>>..." << endl;
bse::vector<bse::unique_ptr<int>> vec;
log.info() << "2x insertion" << endl;
vec.push_back(bse::make_unique<int>(1));
vec.push_back(bse::make_unique<int>(2));
log.info() << "Leaving scope" << endl;
}
log.info() << "Should be deleted by now..." << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,15 @@
#ifndef SmartPointerDemo_include__
#define SmartPointerDemo_include__
#include "kernel/system/Globals.h"
class SmartPointerDemo : public Thread {
public:
SmartPointerDemo(const SmartPointerDemo& copy) = delete;
SmartPointerDemo() : Thread("SmartPointerDemo") {}
void run() override;
};
#endif

View File

@ -0,0 +1,56 @@
#include "StringDemo.h"
void StringDemo::run() {
kout.lock();
kout.clear();
log.info() << "Allocating new string" << endl;
bse::string str1 = "This is a dynamically allocated string!";
kout << str1 << endl;
log.info() << "Reassign string" << endl;
str1 = "Hello";
kout << str1 << " has length " << dec << str1.size() << endl;
kout << "Again with strlen: Hello has length " << dec << bse::strlen("Hello") << endl;
kout << "Adding strings: " << str1 << " + World" << endl;
log.info() << "Adding strings" << endl;
str1 = str1 + " World";
kout << str1 << endl;
kout << "Hello += World" << endl;
log.info() << "Hello += World" << endl;
bse::string str3 = "Hello";
str3 += " World";
kout << str3 << endl;
kout << "Hello World *= 3" << endl;
str3 *= 3;
kout << str3 << endl;
kout << "String iterator!" << endl;
for (const char c : str1) {
kout << c << " ";
}
kout << endl;
log.info() << "Allocating new string" << endl;
bse::string str2 = "Hello World";
kout << "str1 == str2: " << static_cast<int>(str1 == str2) << endl;
kout << "strcmp(Hello, Hello): " << bse::strcmp("Hello", "Hello") << endl;
log.info() << "Reassign str2" << endl;
str2 = "Hello";
bse::array<char, 5> arr{};
arr[0] = 'H';
arr[1] = 'e';
arr[2] = 'l';
arr[3] = 'l';
arr[4] = 'o';
kout << "bse::array<char, 5> to bse::string: " << static_cast<bse::string>(arr) << ", size: " << (bse::string(arr)).size() << endl;
kout << "(bse::string)arr (" << static_cast<bse::string>(arr) << ") == str2 (" << str2 << "): " << static_cast<int>(bse::string(arr) == str2) << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,15 @@
#ifndef StringDemo_include__
#define StringDemo_include__
#include "kernel/system/Globals.h"
class StringDemo : public Thread {
public:
StringDemo(const StringDemo& copy) = delete;
StringDemo() : Thread("StringDemo") {}
void run() override;
};
#endif

43
src/kernel/demo/TextDemo.cc Executable file
View File

@ -0,0 +1,43 @@
/*****************************************************************************
* *
* T E X T D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Testausgaben für den CGA-Treiber. *
* *
* Autor: Michael Schoettner, HHU, 26.10.2018 *
*****************************************************************************/
#include "TextDemo.h"
void TextDemo::run() {
/* Hier muess Code eingefuegt werden */
kout.lock();
kout.clear();
kout << "TextDemo\n"
<< endl;
kout << "Attribut (GREEN on WHITE): "
<< bgc(CGA::WHITE) << green << "GREEN on WHITE" << endl
<< "Attribut (WHITE on BLACK): "
<< bgc(CGA::BLACK) << white << "WHITE on BLACK" << endl;
kout << endl;
kout << "Test der Zahlenausgabefunktion:" << endl
<< "| dec | hex | bin |" << endl
<< "+-------+-------+-------+" << endl;
for (unsigned short num = 0; num < 17; ++num) {
kout << fillw(0) << "| " << fillw(6) << dec << num
<< fillw(0) << "| " << fillw(6) << hex << num
<< fillw(0) << "| " << fillw(6) << bin << num
<< fillw(0) << "|" << endl;
}
kout << endl;
kout.unlock();
scheduler.exit();
}

26
src/kernel/demo/TextDemo.h Executable file
View File

@ -0,0 +1,26 @@
/*****************************************************************************
* *
* T E X T D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Testausgaben für den CGA-Treiber. *
* *
* Autor: Michael Schoettner, HHU, 26.10.2018 *
*****************************************************************************/
#ifndef TextDemo_include__
#define TextDemo_include__
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
class TextDemo : public Thread {
public:
TextDemo(const TextDemo& copy) = delete;
TextDemo() : Thread("TextDemo") {}
void run() override;
};
#endif

106
src/kernel/demo/VBEdemo.cc Normal file
View File

@ -0,0 +1,106 @@
/*****************************************************************************
* *
* V B E D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Demo zu VESA. *
* *
* Autor: Michael Schoettner, HHU, 26.12.2016 *
*****************************************************************************/
#include "VBEdemo.h"
#include "bmp_hhu.cc"
/*****************************************************************************
* Methode: VBEdemo::linInterPol1D *
*---------------------------------------------------------------------------*
* Beschreibung: Farbwert in einer Dimension interpoliert berechnen. *
*****************************************************************************/
int VBEdemo::linInterPol1D(int x, int xr, int l, int r) {
return ((((l >> 16) * (xr - x) + (r >> 16) * x) / xr) << 16) | (((((l >> 8) & 0xFF) * (xr - x) + ((r >> 8) & 0xFF) * x) / xr) << 8) | (((l & 0xFF) * (xr - x) + (r & 0xFF) * x) / xr);
}
/*****************************************************************************
* Methode: VBEdemo::linInterPol2D *
*---------------------------------------------------------------------------*
* Beschreibung: Farbwert in zwei Dimensionen interpoliert berechnen. *
*****************************************************************************/
int VBEdemo::linInterPol2D(int x, int y, int lt, int rt, int lb, int rb) {
return linInterPol1D(y, vesa.yres,
linInterPol1D(x, vesa.xres, lt, rt),
linInterPol1D(x, vesa.xres, lb, rb));
}
/*****************************************************************************
* Methode: VBEdemo::drawColors *
*---------------------------------------------------------------------------*
* Beschreibung: Pixel-Demo. *
*****************************************************************************/
void VBEdemo::drawColors() {
int x_res = 640;
int y_res = 480;
for (int y = 0; y < y_res; y++) {
for (int x = 0; x < x_res; x++) {
vesa.drawPixel(x, y, linInterPol2D(x, y, 0x0000FF, 0x00FF00, 0xFF0000, 0xFFFF00));
}
}
}
/*****************************************************************************
* Methode: VBEdemo::drawBitmap *
*---------------------------------------------------------------------------*
* Beschreibung: Bitmap aus GIMP ausgeben. *
*****************************************************************************/
void VBEdemo::drawBitmap() {
unsigned int sprite_width = hhu.width;
unsigned int sprite_height = hhu.height;
unsigned int sprite_bpp = hhu.bytes_per_pixel;
const unsigned char* sprite_pixel = reinterpret_cast<const unsigned char*>(hhu.pixel_data);
/* Hier muss Code eingefuegt werden */
vesa.drawSprite(sprite_width, sprite_height, sprite_bpp, sprite_pixel);
}
/*****************************************************************************
* Methode: VBEdemo::drawFonts *
*---------------------------------------------------------------------------*
* Beschreibung: Fonts ausgeben. *
*****************************************************************************/
void VBEdemo::drawFonts() {
/* Hier muss Code eingefuegt werden */
vesa.drawString(std_font_8x8, 0, 300, 0, "STD FONT 8x8", 12);
vesa.drawString(std_font_8x16, 0, 320, 0, "STD FONT 8x16", 13);
vesa.drawString(acorn_font_8x8, 0, 340, 0, "ACORN FONT 8x8", 14);
vesa.drawString(sun_font_8x16, 0, 360, 0, "SUN FONT 8x16", 13);
vesa.drawString(sun_font_12x22, 0, 380, 0, "SUN FONT 12x22", 14);
vesa.drawString(pearl_font_8x8, 0, 400, 0, "PEARL FONT 8x8", 14);
}
/*****************************************************************************
* Methode: VBEdemo::run *
*---------------------------------------------------------------------------*
* Beschreibung: Der Anwendungsthread erzeugt drei Threads die Zaehler *
* ausgeben und terminiert sich selbst. *
*****************************************************************************/
void VBEdemo::run() {
// In den Grafikmodus schalten (32-Bit Farbtiefe)
vesa.initGraphicMode(MODE_640_480_24BITS);
vesa.setDrawingBuff(BUFFER_VISIBLE);
drawColors();
/* Hier muss Code eingefuegt werden */
vesa.drawRectangle(100, 100, 300, 300, 0);
drawBitmap();
drawFonts();
while (running) {}
// selbst terminieren
scheduler.exit();
}

46
src/kernel/demo/VBEdemo.h Normal file
View File

@ -0,0 +1,46 @@
/*****************************************************************************
* *
* V B E D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Demo zu VESA. *
* *
* Autor: Michael Schoettner, HHU, 26.12.2016 *
*****************************************************************************/
#ifndef VBEdemo_include__
#define VBEdemo_include__
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
class VBEdemo : public Thread {
private:
// Hilfsfunktionen fuer drawColors()
static int linInterPol1D(int x, int xr, int l, int r);
static int linInterPol2D(int x, int y, int lt, int rt, int lb, int rb);
public:
VBEdemo(const VBEdemo& copy) = delete; // Verhindere Kopieren
// Gib dem Anwendungsthread einen Stack.
VBEdemo() : Thread("VBEdemo") {}
~VBEdemo() override {
allocator.free(reinterpret_cast<void*>(vesa.hfb)); // Memory is allocated after every start and never deleted, so add that
VESA::initTextMode();
}
// Thread-Startmethode
void run() override;
// Farbraum ausgeben
void drawColors();
// Bitmap aus GIMP ausgeben
static void drawBitmap();
// Fonts ausgeben
static void drawFonts();
};
#endif

View File

@ -0,0 +1,110 @@
#include "VectorDemo.h"
void print(OutStream& os, const bse::vector<int>& list) {
os << "Printing List: ";
for (const int i : list) {
os << i << " ";
}
os << endl;
}
void VectorDemo::run() {
bse::vector<int> list;
kout.lock();
kout.clear();
kout << "Logs are written to serial to see the memory interactions" << endl;
log.info() << "Initial list size: " << dec << list.size() << endl;
log.info() << "Adding elements in order" << endl;
for (int i = 0; i < 5; ++i) {
list.push_back(i);
}
print(log.info(), list);
log.info() << "Removing all elements from the front" << endl;
for (unsigned int i = 0; i < 5; ++i) {
list.erase(list.begin());
}
print(log.info(), list);
// ============================================================
log.info() << "Adding elements in order with realloc" << endl;
for (int i = 0; i < 10; ++i) {
log.info() << "Add " << dec << i << endl;
list.push_back(i);
}
print(log.info(), list);
log.info() << "Removing all elements from the back" << endl;
for (unsigned int i = 0; i < 10; ++i) {
list.erase(list.end() - 1);
}
print(log.info(), list);
// ============================================================
for (int i = 0; i < 5; ++i) {
list.push_back(i);
}
print(log.info(), list);
log.info() << "Adding inside the list (at idx 0, 2, 5)" << endl;
list.insert(list.begin() + 0, 10);
list.insert(list.begin() + 2, 10);
list.insert(list.begin() + 5, 10);
print(log.info(), list);
log.info() << "Removing inside the list (at idx 0, 2, 5)" << endl;
list.erase(list.begin() + 0);
list.erase(list.begin() + 2);
list.erase(list.begin() + 5);
print(log.info(), list);
for (unsigned int i = 0; i < 5; ++i) {
list.erase(list.begin());
}
print(log.info(), list);
// ============================================================
log.info() << "Mirror scheduling behavior" << endl;
// These are the threads
int active = 0; // Idle thread
list.push_back(1);
list.push_back(2);
list.push_back(3);
print(log.info(), list);
log.info() << "Starting..." << endl;
for (unsigned int n = 0; n < 10000; ++n) {
list.push_back(active);
active = list[0];
list.erase(list.begin());
if (list.size() != 3 || active == -1) {
log.info() << "ERROR: Thread went missing" << endl;
break;
}
if (n < 5) {
print(log.info(), list);
}
}
log.info() << "Finished." << endl;
print(log.info(), list);
// ============================================================
log.info() << "Range based for support" << endl;
for (int i : list) {
log.info() << "List contains element: " << dec << i << endl;
}
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,17 @@
#ifndef VectorDemo_include__
#define VectorDemo_include__
#include "kernel/system/Globals.h"
#include "kernel/process/Thread.h"
#include "lib/util/Vector.h"
class VectorDemo : public Thread {
public:
VectorDemo(const VectorDemo& copy) = delete;
VectorDemo() : Thread("VectorDemo") {}
void run() override;
};
#endif

1576
src/kernel/demo/bmp_hhu.cc Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
#include "kernel/event/KeyEventListener.h"
#include "kernel/system/Globals.h"
void KeyEventListener::trigger(char c) {
lastChar = c;
}
char KeyEventListener::waitForKeyEvent() const {
Logger::instance() << DEBUG << "KEvLis:: Thread with id: " << tid << " waiting for key event" << endl;
scheduler.block();
return lastChar; // This is only executed after thread is woken up by manager
}

View File

@ -0,0 +1,22 @@
#ifndef KeyEventListener_Include_H_
#define KeyEventListener_Include_H_
#include "kernel/process/Thread.h"
class KeyEventListener {
private:
char lastChar = '\0';
friend class KeyEventManager;
unsigned int tid; // Thread which contains this listener, so the listener can block the thread
public:
KeyEventListener(const KeyEventListener& copy) = delete;
KeyEventListener(unsigned int tid) : tid(tid) {}
char waitForKeyEvent() const; // Blocks the thread until woken up by manager
void trigger(char c); // Gets called from KeyEventManager
};
#endif

View File

@ -0,0 +1,26 @@
#include "KeyEventManager.h"
#include "kernel/system/Globals.h"
void KeyEventManager::subscribe(KeyEventListener& sub) {
log.debug() << "Subscribe, Thread ID: " << dec << sub.tid << endl;
listeners.push_back(&sub);
}
void KeyEventManager::unsubscribe(KeyEventListener& unsub) {
log.debug() << "Unsubscribe, Thread ID: " << dec << unsub.tid << endl;
for (bse::vector<KeyEventListener*>::iterator it = listeners.begin(); it != listeners.end(); ++it) {
if ((*it)->tid == unsub.tid) {
listeners.erase(it);
return;
}
}
}
void KeyEventManager::broadcast(char c) {
log.trace() << "Beginning Broadcast" << endl;
for (KeyEventListener* listener : listeners) {
log.trace() << "Broadcasting " << c << " to Thread ID: " << dec << listener->tid << endl;
listener->trigger(c);
scheduler.deblock(listener->tid);
}
}

View File

@ -0,0 +1,31 @@
#ifndef KeyEventManager_Include_H_
#define KeyEventManager_Include_H_
#include "kernel/event/KeyEventListener.h"
#include "kernel/log/Logger.h"
#include "lib/util/Vector.h"
// NOTE: Could do this more generally but we only have key events
// Also pretty limited: One thread can have one listener as identification is done over tid
class KeyEventManager {
private:
NamedLogger log;
bse::vector<KeyEventListener*> listeners;
public:
KeyEventManager(const KeyEventManager& copy) = delete;
KeyEventManager() : log("KEvMan"), listeners(true) {}
void init() {
listeners.reserve();
}
void subscribe(KeyEventListener& sub);
void unsubscribe(KeyEventListener& unsub);
void broadcast(char c); // Unblocks all input waiting threads, I don't have a method to direct input
};
#endif

View File

@ -0,0 +1,305 @@
/*****************************************************************************
* *
* B L U E S C R E E N *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Ein Bluescreen, falls eine x86 Exception auftritt. Evt. *
* ist der Stack und oder Heap kaputt, weswegen hier nicht *
* kout etc. verwendet wird. *
* *
* Autor: Michael Schoettner, 11.12.2018 *
*****************************************************************************/
#include "kernel/system/Globals.h"
// in startup.asm
extern "C" {
// CR2 auslesen
unsigned int get_page_fault_address();
// 1st level interrupt handler in startup.asm sichert Zeiger auf Stackframe
// unmittelbar nach dem Interrupt und nachdem alle Register mit PUSHAD
// gesichert wurden
// |-------------|
// |  EFLAGS |
// |-------------|
// | CS |
// |-------------|
// | EIP |
// |-------------|
// | [ErrorCode] |
// |-------------|
// | EAX |
// |-------------|
// | ECX |
// |-------------|
// | EDX |
// |-------------|
// | EBX |
// |-------------|
// | ESP |
// |-------------|
// | EBP |
// |-------------|
// | ESI |
// |-------------|
// | EDI |
// |-------------| <-- int_esp
void get_int_esp(unsigned int** esp);
}
void break_on_bluescreen() {
/* wenn auf diese Methode ein breakpoint in GDB gesetzt wird
so kann man sich mithilfe des Hex-Dumps umsehen
*/
}
// Cursor-Position
int bs_xpos = 0;
int bs_ypos = 0;
/*****************************************************************************
* Funktion: bs_clear *
*---------------------------------------------------------------------------*
* Beschreibung: Bildschirm loeschen. *
*****************************************************************************/
void bs_clear() {
unsigned int x;
unsigned int y;
unsigned short* ptr = reinterpret_cast<unsigned short*>(0xb8000);
for (x = 0; x < 80; x++) {
for (y = 0; y < 25; y++) {
*(ptr + y * 80 + x) = static_cast<short>(0x1F00);
}
}
bs_xpos = 0;
bs_ypos = 0;
}
/*****************************************************************************
* Funktion: bs_lf *
*---------------------------------------------------------------------------*
* Beschreibung: Zeilenvorschub. *
*****************************************************************************/
void bs_lf() {
bs_ypos++;
bs_xpos = 0;
}
/*****************************************************************************
* Funktion: bs_print_char *
*---------------------------------------------------------------------------*
* Beschreibung: Ein Zeichen ausgeben. *
*****************************************************************************/
void bs_print_char(char c) {
unsigned char* ptr = reinterpret_cast<unsigned char*>(0xb8000);
*(ptr + bs_ypos * 80 * 2 + bs_xpos * 2) = c;
bs_xpos++;
}
/*****************************************************************************
* Funktion: bs_print_string *
*---------------------------------------------------------------------------*
* Beschreibung: Eine Zeichenkette ausgeben. *
*****************************************************************************/
void bs_print_string(char* str) {
while (*str != '\0') {
bs_print_char(*str);
str++;
}
}
/*****************************************************************************
* Funktion: bs_printHexDigit *
*---------------------------------------------------------------------------*
* Beschreibung: Ein Hex-Zeichen ausgeben. *
*****************************************************************************/
void bs_printHexDigit(int c) {
if (c < 10) {
bs_print_char('0' + static_cast<unsigned char>(c));
} else {
bs_print_char('A' + static_cast<unsigned char>(c - 10));
}
}
/*****************************************************************************
* Funktion: bs_print_uintHex *
*---------------------------------------------------------------------------*
* Beschreibung: Integer ausgeben. *
*****************************************************************************/
void bs_print_uintHex(unsigned int c) {
for (int i = 28; i >= 0; i = i - 4) {
bs_printHexDigit((c >> i) & 0xF);
}
}
/*****************************************************************************
* Funktion: bs_printReg *
*---------------------------------------------------------------------------*
* Beschreibung: String mit Integer ausgeben. Wird verwendet um ein *
* Register auszugeben. *
*****************************************************************************/
void bs_printReg(char* str, unsigned int value) {
bs_print_string(str);
bs_print_uintHex(value);
bs_print_string(" \0");
}
/*****************************************************************************
* Funktion: bs_dump *
*---------------------------------------------------------------------------*
* Beschreibung: Hauptroutine des Bluescreens. *
*****************************************************************************/
void bs_dump(unsigned int exceptionNr) {
unsigned int* int_esp;
unsigned int* sptr;
unsigned int faultAdress;
unsigned int has_error_code = 0;
bs_clear();
bs_print_string("HHUos crashed with Exception \0");
// Exception mit Error-Code?
if ((exceptionNr >= 8 && exceptionNr <= 14) || exceptionNr == 17 || exceptionNr == 30) {
has_error_code = 1;
}
// Liegt ein Page-Fault vor?
if (exceptionNr == 14) {
faultAdress = get_page_fault_address();
// Zugriff auf Seite 0 ? -> Null-Ptr. Exception
if ((faultAdress & 0xFFFFF000) == 0) {
exceptionNr = 0x1B;
}
}
bs_print_uintHex(exceptionNr);
bs_print_string(" (\0");
// Spruch ausgeben
switch (exceptionNr) {
case 0x00: bs_print_string("Divide Error\0"); break;
case 0x01: bs_print_string("Debug Exception\0"); break;
case 0x02: bs_print_string("NMI\0"); break;
case 0x03: bs_print_string("Breakpoint Exception\0"); break;
case 0x04: bs_print_string("Into Exception\0"); break;
case 0x05: bs_print_string("Index out of range Exception\0"); break;
case 0x06: bs_print_string("Invalid Opcode\0"); break;
case 0x08: bs_print_string("Double Fault\0"); break;
case 0x0D: bs_print_string("General Protection Error\0"); break;
case 0x0E: bs_print_string("Page Fault\0"); break;
case 0x18: bs_print_string("Stack invalid\0"); break;
case 0x19: bs_print_string("Return missing\0"); break;
case 0x1A: bs_print_string("Type Test Failed\0"); break;
case 0x1B: bs_print_string("Null pointer exception\0"); break;
case 0x1C: bs_print_string("MAGIC.StackTest failed\0"); break;
case 0x1D: bs_print_string("Memory-Panic\0"); break;
case 0x1E: bs_print_string("Pageload failed\0"); break;
case 0x1F: bs_print_string("Stack overflow\0"); break;
default: bs_print_string("unknown\0");
}
bs_print_string(")\0");
bs_lf();
// Zeiger auf int_esp ueber startup.asm beschaffen (Stack-Layout siehe Anfang dieser Datei)
get_int_esp(&int_esp);
// wir müssen den Inhalt auslesen und das als Zeiger verwenden, um den Stack auszulesen
sptr = reinterpret_cast<unsigned int*>(*int_esp);
bs_lf();
// wichtigste Register ausgeben
// Exception mit Error-Code?
bs_printReg("EIP=\0", *(sptr + 8 + has_error_code));
bs_printReg("EBP=\0", *(sptr + 2));
bs_printReg("ESP=\0", *(sptr + 3));
bs_printReg(" CS=\0", *(sptr + 9 + has_error_code));
bs_lf();
// verbleibende nicht-fluechtige Register ausgeben
bs_printReg("EBX=\0", *(sptr + 4));
bs_printReg("ESI=\0", *(sptr + 1));
bs_printReg("EDI=\0", *(sptr));
bs_lf();
// verbleibende fluechtige Register ausgeben
bs_printReg("EDX=\0", *(sptr + 5));
bs_printReg("ECX=\0", *(sptr + 6));
bs_printReg("EAX=\0", *(sptr + 7));
bs_printReg("EFL=\0", *(sptr + 10));
bs_lf();
// Pagefault oder Null-Pointer?
if (exceptionNr == 14 || exceptionNr == 0x1B) {
bs_lf();
bs_print_string("Fault address = \0");
bs_print_uintHex(faultAdress);
bs_lf();
bs_print_string("Last useable address = \0");
bs_print_uintHex(total_mem - 1);
bs_lf();
}
// Exception mit Error-Code?
if (has_error_code == 1) {
unsigned int error_nr = *(sptr + 8);
if (exceptionNr == 14) {
if (error_nr == 3) {
bs_print_string("Error: write access to read-only page.\0");
} else if (error_nr == 2) {
bs_print_string("Error: read access to not-present page.\0");
} else if (error_nr == 0) {
bs_print_string("Error: access to a not-present page.\0");
} else {
bs_print_string("Error code = \0");
bs_print_uintHex(error_nr);
}
bs_lf();
} else {
bs_print_string("Error code = \0");
bs_print_uintHex(error_nr);
bs_lf();
}
}
// Calling stack ...
bs_lf();
bs_print_string("Calling Stack:\0");
bs_lf();
int x = 0;
unsigned int* ebp = reinterpret_cast<unsigned int*>(*(sptr + 2));
unsigned int raddr;
// solange eip > 1 MB && ebp < 128 MB, max. Aufruftiefe 10
while (*ebp > 0x100000 && *ebp < 0x8000000 && x < 10) {
raddr = *(ebp + 1);
bs_printReg(" raddr=\0", raddr);
bs_lf();
// dynamische Kette -> zum Aufrufer
ebp = reinterpret_cast<unsigned int*>(*ebp);
x++;
}
if (x == 0) {
bs_print_string(" empty\0");
bs_lf();
}
bs_lf();
// nur falls gdb benutzt werden soll
break_on_bluescreen();
bs_print_string("System halted\0");
}

View File

@ -0,0 +1,19 @@
/*****************************************************************************
* *
* B L U E S C R E E N *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Ein Bluescreen, falls eine x86 Exception auftritt. Evt. *
* ist der Stack und oder Heap kaputt, weswegen hier nicht *
* kout etc. verwendet wird. *
* *
* Autor: Michael Schoettner, 2.2.2017 *
*****************************************************************************/
#ifndef Bluescreen_include__
#define Bluescreen_include__
// dump blue screen (will not return)
void bs_dump(unsigned int exceptionNr);
#endif

27
src/kernel/interrupt/ISR.h Executable file
View File

@ -0,0 +1,27 @@
/*****************************************************************************
* *
* I S R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Interrupt Service Routine. Jeweils ein Objekt pro ISR. *
* Erlaubt es einen Kontext mit Variablen fuer die Unter- *
* brechungsroutine bereitzustellen. *
* *
* Autor: Michael Schoettner, 06.04.20 *
*****************************************************************************/
#ifndef ISR_include__
#define ISR_include__
class ISR {
public:
ISR(const ISR& copy) = delete; // Verhindere Kopieren
// virtual ~ISR() = default;
ISR() = default;
// Unterbrechungsbehandlungsroutine
virtual void trigger() = 0;
};
#endif

View File

@ -0,0 +1,109 @@
/*****************************************************************************
* *
* I N T D I S P A T C H E R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Zentrale Unterbrechungsbehandlungsroutine des Systems. *
* Der Parameter gibt die Nummer des aufgetretenen *
* Interrupts an. Wenn eine Interrupt Service Routine *
* registriert ist, wird diese aufgerufen. *
* *
* Autor: Michael Schoettner, 31.8.2016 *
*****************************************************************************/
#include "IntDispatcher.h"
#include "device/cpu/CPU.h"
#include "kernel/system/Globals.h"
#include "kernel/interrupt/Bluescreen.h"
extern "C" void int_disp(unsigned int vector);
/*****************************************************************************
* Prozedur: int_disp *
*---------------------------------------------------------------------------*
* Beschreibung: Low-Level Interrupt-Behandlung. *
* Diese Funktion ist in der IDT fuer alle Eintraege einge- *
* tragen. Dies geschieht bereits im Bootloader. *
* Sie wird also fuer alle Interrupts aufgerufen. Von hier *
* aus sollen die passenden ISR-Routinen der Treiber-Objekte*
* mithilfe von 'IntDispatcher::report' aufgerufen werden. *
* Parameter: *
* vector: Vektor-Nummer der Unterbrechung *
*****************************************************************************/
void int_disp(unsigned int vector) {
/* hier muss Code eingefuegt werden */
if (vector < 32) {
bs_dump(vector);
CPU::halt();
}
if (intdis.report(vector) < 0) {
kout << "Panic: unexpected interrupt " << vector;
kout << " - processor halted." << endl;
CPU::halt();
}
}
/*****************************************************************************
* Methode: IntDispatcher::assign *
*---------------------------------------------------------------------------*
* Beschreibung: Registrierung einer ISR. *
* *
* Parameter: *
* vector: Vektor-Nummer der Unterbrechung *
* isr: ISR die registriert werden soll *
* *
* Rueckgabewert: 0 = Erfolg, -1 = Fehler *
*****************************************************************************/
int IntDispatcher::assign(unsigned int vector, ISR& isr) {
/* hier muss Code eingefuegt werden */
if (vector >= size) {
log.error() << "Invalid vector number when assigning" << endl;
return -1;
}
map[vector] = &isr;
log.info() << "Registered ISR for vector " << dec << vector << endl;
return 0;
}
/*****************************************************************************
* Methode: IntDispatcher::report *
*---------------------------------------------------------------------------*
* Beschreibung: Eingetragene ISR ausfuehren. *
* *
* Parameter: *
* vector: Gesuchtes ISR-Objekt fuer diese Vektor-Nummer. *
* *
* Rueckgabewert: 0 = ISR wurde aufgerufen, -1 = unbekannte Vektor-Nummer *
*****************************************************************************/
int IntDispatcher::report(unsigned int vector) {
/* hier muss Code eingefuegt werden */
if (vector >= size) {
return -1;
}
ISR* isr = map[vector];
if (isr == nullptr) {
log.error() << "No ISR registered for vector " << vector << endl;
return -1;
}
// 32 = Timer
// 33 = Keyboard
// log.trace() << "Interrupt: " << dec << vector << endl;
if (vector == 33) {
log.debug() << "Keyboard Interrupt" << endl;
}
isr->trigger();
return 0;
}

View File

@ -0,0 +1,51 @@
/*****************************************************************************
* *
* I N T D I S P A T C H E R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Zentrale Unterbrechungsbehandlungsroutine des Systems. *
* Der Parameter gibt die Nummer des aufgetretenen *
* Interrupts an. Wenn eine Interrupt Service Routine (ISR) *
* in der Map registriert ist, so wird diese aufgerufen. *
* *
* Autor: Michael Schoettner, 30.7.16 *
*****************************************************************************/
#ifndef IntDispatcher_include__
#define IntDispatcher_include__
#include "ISR.h"
#include "lib/util/Array.h"
#include "kernel/log/Logger.h"
class IntDispatcher {
private:
NamedLogger log;
enum { size = 256 };
bse::array<ISR*, size> map;
public:
IntDispatcher(const IntDispatcher& copy) = delete; // Verhindere Kopieren
// Vektor-Nummern
enum {
timer = 32,
keyboard = 33,
com1 = 36
};
// Initialisierung der ISR map mit einer Default-ISR.
IntDispatcher() : log("IntDis") {
for (ISR*& slot : map) {
slot = nullptr;
}
}
// Registrierung einer ISR. (Rueckgabewert: 0 = Erfolg, -1 = Fehler)
int assign(unsigned int vector, ISR& isr);
// ISR fuer 'vector' ausfuehren
int report(unsigned int vector);
};
#endif

117
src/kernel/log/Logger.cc Normal file
View File

@ -0,0 +1,117 @@
#include "Logger.h"
#include "kernel/system/Globals.h"
bool Logger::kout_enabled = true;
bool Logger::serial_enabled = true;
Logger::LogLevel Logger::level = Logger::ERROR;
constexpr const char* ansi_red = "\033[1;31m";
constexpr const char* ansi_green = "\033[1;32m";
constexpr const char* ansi_yellow = "\033[1;33m";
constexpr const char* ansi_blue = "\033[1;34m";
constexpr const char* ansi_magenta = "\033[1;35m";
constexpr const char* ansi_cyan = "\033[1;36m";
constexpr const char* ansi_white = "\033[1;37m";
constexpr const char* ansi_default = "\033[0;39m ";
void Logger::log(const bse::string_view message, CGA::color col) const {
if (Logger::kout_enabled) {
CGA::color old_col = kout.color_fg;
kout << fgc(col)
<< Logger::level_to_string(current_message_level) << "::"
<< message << fgc(old_col);
kout.flush(); // Don't add newline, Logger already does that
}
if (Logger::serial_enabled) {
switch (col) {
case CGA::WHITE:
SerialOut::write(ansi_white);
break;
case CGA::LIGHT_MAGENTA:
SerialOut::write(ansi_magenta);
break;
case CGA::LIGHT_RED:
SerialOut::write(ansi_red);
break;
case CGA::LIGHT_BLUE:
SerialOut::write(ansi_blue);
break;
default:
SerialOut::write(ansi_default);
}
SerialOut::write(Logger::level_to_string(current_message_level));
SerialOut::write(":: ");
SerialOut::write(message);
SerialOut::write('\r');
// serial.write("\r\n");
}
}
void Logger::flush() {
buffer[pos] = '\0';
switch (current_message_level) {
case Logger::TRACE:
trace(buffer.data());
break;
case Logger::DEBUG:
debug(buffer.data());
break;
case Logger::ERROR:
error(buffer.data());
break;
case Logger::INFO:
info(buffer.data());
break;
}
current_message_level = Logger::INFO;
pos = 0;
Logger::unlock();
}
void Logger::trace(const bse::string_view message) const {
if (Logger::level <= Logger::TRACE) {
log(message, CGA::WHITE);
}
}
void Logger::debug(const bse::string_view message) const {
if (Logger::level <= Logger::DEBUG) {
log(message, CGA::LIGHT_MAGENTA);
}
}
void Logger::error(const bse::string_view message) const {
if (Logger::level <= Logger::ERROR) {
log(message, CGA::LIGHT_RED);
}
}
void Logger::info(const bse::string_view message) const {
if (Logger::level <= Logger::INFO) {
log(message, CGA::LIGHT_BLUE);
}
}
// Manipulatoren
Logger& TRACE(Logger& log) {
log.current_message_level = Logger::TRACE;
return log;
}
Logger& DEBUG(Logger& log) {
log.current_message_level = Logger::DEBUG;
return log;
}
Logger& ERROR(Logger& log) {
log.current_message_level = Logger::ERROR;
return log;
}
Logger& INFO(Logger& log) {
log.current_message_level = Logger::INFO;
return log;
}

121
src/kernel/log/Logger.h Normal file
View File

@ -0,0 +1,121 @@
#ifndef Logger_Include_H_
#define Logger_Include_H_
#include "device/graphics/CGA.h"
#include "lib/stream/OutStream.h"
#include "lib/async/SpinLock.h"
#include "lib/util/String.h"
#include "lib/util/StringView.h"
class Logger : public OutStream {
public:
static Logger& instance() {
static Logger log;
return log;
}
private:
Logger() = default;
static bool kout_enabled;
static bool serial_enabled;
void log(const bse::string_view message, CGA::color col) const;
friend class NamedLogger; // Allow NamedLogger to lock/unlock
SpinLock sem; // Semaphore would be a cyclic include
static void lock() { Logger::instance().sem.acquire(); }
static void unlock() { Logger::instance().sem.release(); }
// static void lock() {}
// static void unlock() {}
public:
// ~Logger() override = default;
Logger(const Logger& copy) = delete;
void operator=(const Logger& copy) = delete;
enum LogLevel {
TRACE,
DEBUG,
ERROR,
INFO
};
static LogLevel level;
LogLevel current_message_level = Logger::INFO; // Use this to log with manipulators
void flush() override;
void trace(const bse::string_view message) const;
void debug(const bse::string_view message) const;
void error(const bse::string_view message) const;
void info(const bse::string_view message) const;
// TODO: Make lvl change accessible over menu
static void set_level(LogLevel lvl) {
Logger::level = lvl;
}
static bse::string_view level_to_string(LogLevel lvl) {
switch (lvl) {
case Logger::TRACE:
return "TRACE";
case Logger::DEBUG:
return "DEBUG";
case Logger::ERROR:
return "ERROR";
case Logger::INFO:
return "INFO";
}
}
static void enable_kout() {
Logger::kout_enabled = true;
}
static void disable_kout() {
Logger::kout_enabled = false;
}
static void enable_serial() {
Logger::serial_enabled = true;
}
static void disable_serial() {
Logger::serial_enabled = false;
}
};
// Manipulatoren
Logger& TRACE(Logger& log);
Logger& DEBUG(Logger& log);
Logger& ERROR(Logger& log);
Logger& INFO(Logger& log);
class NamedLogger {
private:
const char* name;
public:
explicit NamedLogger(const char* name) : name(name) {}
Logger& trace() {
Logger::lock();
return Logger::instance() << TRACE << name << "::";
}
Logger& debug() {
Logger::lock();
return Logger::instance() << DEBUG << name << "::";
}
Logger& error() {
Logger::lock();
return Logger::instance() << ERROR << name << "::";
}
Logger& info() {
Logger::lock();
return Logger::instance() << INFO << name << "::";
}
};
#endif

80
src/kernel/memory/Allocator.cc Executable file
View File

@ -0,0 +1,80 @@
/*****************************************************************************
* *
* A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung. 'new' und 'delete' werden *
* durch Ueberladen der entsprechenden Operatoren *
* realisiert. *
* *
* Memory-Laylout *
* *
* boot.asm *
* 0x07c0: Bootsector vom BIOS geladen *
* 0x0060: Boot-Code verschiebt sich hier hin *
* 0x9000: Setup-Code (max. 64K inkl. Stack) vom *
* Bootsector-Code geladen *
* setup.asm *
* 0x1000: System-Code (max. 512K) geladen *
* System-Code *
* 0x100000: System-Code, kopiert nach Umschalten in *
* den Protected Mode kopiert (GRUB kann nur *
* an Adressen >1M laden) *
* Globale Variablen: Direkt nach dem Code liegen die globalen *
* Variablen. *
* Heap: *
* 0x300000: Start-Adresse der Heap-Verwaltung *
* 0x400000: Letzte Adresse des Heaps *
* *
* Achtung: Benötigt einen PC mit mindestens 8 MB RAM! *
* *
* Autor: Michael Schoettner, HHU, 1.3.2022 *
*****************************************************************************/
#include "Allocator.h"
#include "kernel/system/Globals.h"
constexpr const unsigned int MEM_SIZE_DEF = 8 * 1024 * 1024; // Groesse des Speichers = 8 MB
constexpr const unsigned int HEAP_START = 0x300000; // Startadresse des Heaps
constexpr const unsigned int HEAP_SIZE = 1024 * 1024; // Default-Groesse des Heaps, falls \
// nicht über das BIOS ermittelbar
/*****************************************************************************
* Konstruktor: Allocator::Allocator *
*****************************************************************************/
Allocator::Allocator() : heap_start(HEAP_START), heap_end(HEAP_START + HEAP_SIZE), heap_size(HEAP_SIZE), initialized(1) {
// Groesse des Hauptspeichers (kann über das BIOS abgefragt werden,
// aber sehr umstaendlich, daher hier fest eingetragen
total_mem = MEM_SIZE_DEF;
}
/*****************************************************************************
* Nachfolgend sind die Operatoren von C++, die wir hier ueberschreiben *
* und entsprechend 'mm_alloc' und 'mm_free' aufrufen. *
*****************************************************************************/
void* operator new(std::size_t size) {
return allocator.alloc(size);
}
void* operator new[](std::size_t count) {
return allocator.alloc(count);
}
void operator delete(void* ptr) {
allocator.free(ptr);
}
void operator delete[](void* ptr) {
allocator.free(ptr);
}
void operator delete(void* ptr, unsigned int sz) {
allocator.free(ptr);
}
// I don't know if accidentally deleted it but one delete was missing
// https://en.cppreference.com/w/cpp/memory/new/operator_delete
void operator delete[](void* ptr, unsigned int sz) {
allocator.free(ptr);
}

58
src/kernel/memory/Allocator.h Executable file
View File

@ -0,0 +1,58 @@
/*****************************************************************************
* *
* A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung. 'new' und 'delete' werden *
* durch Ueberladen der entsprechenden Operatoren *
* realisiert. *
* *
* Memory-Laylout *
* *
* boot.asm *
* 0x07c0: Bootsector vom BIOS geladen *
* 0x0060: Boot-Code verschiebt sich hier hin *
* 0x9000: Setup-Code (max. 64K inkl. Stack) vom *
* Bootsector-Code geladen *
* setup.asm *
* 0x1000: System-Code (max. 512K) geladen *
* System-Code *
* 0x100000: System-Code, kopiert nach Umschalten in *
* den Protected Mode kopiert (GRUB kann nur *
* an Adressen >1M laden) *
* Globale Variablen: Direkt nach dem Code liegen die globalen *
* Variablen. *
* Heap: *
* 0x300000: Start-Adresse der Heap-Verwaltung *
* 0x400000: Letzte Adresse des Heaps *
* *
* Achtung: Benötigt einen PC mit mindestens 4 MB RAM! *
* *
* Autor: Michael Schoettner, HHU, 13.6.2020 *
*****************************************************************************/
#ifndef Allocator_include__
#define Allocator_include__
constexpr const unsigned int BASIC_ALIGN = 4; // 32 Bit so 4 Bytes?
constexpr const unsigned int HEAP_MIN_FREE_BLOCK_SIZE = 64; // min. Groesse eines freien Blocks
class Allocator {
public:
Allocator(Allocator& copy) = delete; // Verhindere Kopieren
Allocator();
// virtual ~Allocator() = default;
unsigned int heap_start;
unsigned int heap_end;
unsigned int heap_size;
unsigned int initialized;
virtual void init() = 0;
virtual void dump_free_memory() = 0;
virtual void* alloc(unsigned int req_size) = 0;
virtual void free(void* ptr) = 0;
};
#endif

View File

@ -0,0 +1,76 @@
/*****************************************************************************
* *
* B U M P A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Eine sehr einfache Heap-Verwaltung, welche freigegebenen *
* Speicher nicht mehr nutzen kann. *
* *
* Autor: Michael Schoettner, HHU, 3.3.2022 *
*****************************************************************************/
#include "BumpAllocator.h"
#include "kernel/system/Globals.h"
/*****************************************************************************
* Methode: BumpAllocator::init *
*---------------------------------------------------------------------------*
* Beschreibung: BumpAllokartor intitialisieren. *
*****************************************************************************/
void BumpAllocator::init() {
/* Hier muess Code eingefuegt werden */
allocations = 0;
next = reinterpret_cast<unsigned char*>(heap_start);
log.info() << "Initialized Bump Allocator" << endl;
}
/*****************************************************************************
* Methode: BumpAllocator::dump_free_memory *
*---------------------------------------------------------------------------*
* Beschreibung: Ausgabe der Freispeicherinfos. Zu Debuggingzwecken. *
*****************************************************************************/
void BumpAllocator::dump_free_memory() {
/* Hier muess Code eingefuegt werden */
kout << "Freier Speicher:" << endl
<< " - Next: " << hex << reinterpret_cast<unsigned int>(next)
<< ", Allocations: " << dec << allocations << endl;
}
/*****************************************************************************
* Methode: BumpAllocator::alloc *
*---------------------------------------------------------------------------*
* Beschreibung: Einen neuen Speicherblock allozieren. *
*****************************************************************************/
void* BumpAllocator::alloc(unsigned int req_size) {
/* Hier muess Code eingefuegt werden */
log.debug() << "Requested " << hex << req_size << " Bytes" << endl;
if (req_size + reinterpret_cast<unsigned int>(next) > heap_end) {
log.error() << " - More memory requested than available :(" << endl;
return nullptr;
}
void* allocated = next;
next = reinterpret_cast<unsigned char*>(reinterpret_cast<unsigned int>(next) + req_size);
allocations = allocations + 1;
log.trace() << " - Allocated " << hex << req_size << " Bytes." << endl;
return allocated;
}
/*****************************************************************************
* Methode: BumpAllocator::free *
*---------------------------------------------------------------------------*
* Beschreibung: Nicht implementiert. *
*****************************************************************************/
void BumpAllocator::free(void* ptr) {
log.error() << " mm_free: ptr= " << hex << reinterpret_cast<unsigned int>(ptr) << ", not supported" << endl;
}

View File

@ -0,0 +1,38 @@
/*****************************************************************************
* *
* B U M P A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Eine sehr einfache Heap-Verwaltung, welche freigegebenen *
* Speicher nicht mehr nutzen kann. *
* *
* Autor: Michael Schoettner, HHU, 3.3.2022 *
*****************************************************************************/
#ifndef BumpAllocator_include__
#define BumpAllocator_include__
#include "Allocator.h"
#include "kernel/log/Logger.h"
class BumpAllocator : Allocator {
private:
unsigned char* next;
unsigned int allocations;
NamedLogger log;
public:
BumpAllocator(Allocator& copy) = delete; // Verhindere Kopieren
BumpAllocator() : log("BMP-Alloc") {}; // Allocator() called implicitely in C++
// ~BumpAllocator() override = default;
void init() override;
void dump_free_memory() override;
void* alloc(unsigned int req_size) override;
void free(void* ptr) override;
};
#endif

View File

@ -0,0 +1,310 @@
/*****************************************************************************
* *
* L I N K E D L I S T A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung, welche den freien Speicher *
* mithilfe einer einfach verketteten Liste verwaltet. *
* *
* Autor: Michael Schoettner, HHU, 13.6.2020 *
*****************************************************************************/
#include "LinkedListAllocator.h"
#include "kernel/system/Globals.h"
// I don't order the list by size so that the block order corresponds to the location in memory
// Then I can easily merge adjacent free blocks by finding the previous block without looking at
// memory addresses of each block
// (That was the plan at least)
/*****************************************************************************
* Methode: LinkedListAllocator::init *
*---------------------------------------------------------------------------*
* Beschreibung: Liste der Freispeicherbloecke intitialisieren. *
* Anker zeigt auf ein Dummy-Element. Danach folgt *
* ein Block der den gesamten freien Speicher umfasst. *
* *
* Wird automatisch aufgerufen, sobald eine Funktion der *
* Speicherverwaltung erstmalig gerufen wird. *
*****************************************************************************/
void LinkedListAllocator::init() {
/* Hier muess Code eingefuegt werden */
free_start = reinterpret_cast<free_block_t*>(heap_start);
free_start->allocated = false;
free_start->size = heap_size - sizeof(free_block_t);
free_start->next = free_start; // Only one block, points to itself
log.info() << "Initialized LinkedList Allocator" << endl;
}
/*****************************************************************************
* Methode: LinkedListAllocator::dump_free_memory *
*---------------------------------------------------------------------------*
* Beschreibung: Ausgabe der Freispeicherliste. Zu Debuggingzwecken. *
*****************************************************************************/
void LinkedListAllocator::dump_free_memory() {
/* Hier muess Code eingefuegt werden */
kout << "Freier Speicher:" << endl;
if (free_start == nullptr) {
kout << " - No free Blocks" << endl;
} else {
kout << " - Freelist start: " << hex << reinterpret_cast<unsigned int>(free_start) << endl;
free_block_t* current = free_start;
do {
kout << " - Free Block (Start: " << hex << reinterpret_cast<unsigned int>(current)
<< " Size: " << hex << current->size << ")" << endl;
current = current->next;
} while (current != free_start);
}
}
/*****************************************************************************
* Methode: LinkedListAllocator::alloc *
*---------------------------------------------------------------------------*
* Beschreibung: Einen neuen Speicherblock allozieren. *
*****************************************************************************/
void* LinkedListAllocator::alloc(unsigned int req_size) {
lock.acquire();
/* Hier muess Code eingefuegt werden */
// NOTE: next pointer zeigt auf headeranfang, returned wird zeiger auf anfang des nutzbaren freispeichers
log.debug() << "Requested " << hex << req_size << " Bytes" << endl;
if (free_start == nullptr) {
log.error() << " - No free memory remaining :(" << endl;
lock.release();
return nullptr;
}
// Round to word borders
unsigned int req_size_diff = (BASIC_ALIGN - req_size % BASIC_ALIGN) % BASIC_ALIGN;
unsigned int rreq_size = req_size + req_size_diff;
if (req_size_diff > 0) {
log.trace() << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl;
}
free_block_t* current = free_start;
do {
if (current->size >= rreq_size) { // Size doesn't contain header, only usable
// Current block large enough
// We now have: [<> | current | <>]
// Don't subtract to prevent underflow
if (current->size >= rreq_size + sizeof(free_block_t) + HEAP_MIN_FREE_BLOCK_SIZE) {
// Block so large it can be cut
// Create new header after allocated memory and rearrange pointers
// [<> | current | new_next | <>]
// In case of only one freeblock:
// [current | new_next]
free_block_t* new_next =
reinterpret_cast<free_block_t*>(reinterpret_cast<unsigned int>(current) + sizeof(free_block_t) + rreq_size);
// If only one block exists, current->next is current
// This shouldn't be a problem since the block gets removed from the list later
new_next->next = current->next;
new_next->size = current->size - (rreq_size + sizeof(free_block_t));
new_next->allocated = false;
current->next = new_next; // We want to reach the next free block from the allocated block
current->size = rreq_size;
// Next-fit
free_start = new_next;
log.trace() << " - Allocated " << hex << rreq_size << " Bytes with cutting" << endl;
} else {
// Block too small to be cut, allocate whole block
// Next-fit
free_start = current->next; // Pointer keeps pointing to current if last block
if (free_start == current) {
// No free block remaining
log.trace() << " - Disabled freelist" << endl;
free_start = nullptr;
}
log.trace() << " - Allocated " << hex << current->size << " Bytes without cutting" << endl;
}
// Block aushängen
// If block was cut this is obvious, because current->next is a free block
// If block wasn't cut it also works as current was a free block before, so it's next
// block should also be free
free_block_t* previous = LinkedListAllocator::find_previous_block(current);
previous->next = current->next; // Current block was free so previous block pointed at it
current->allocated = true;
// We leave the current->next pointer intact although the block is allocated
// to allow easier merging of adjacent free blocks
// HACK: Checking list integrity
// free_block_t* c = current;
// log.debug() << "Checking list Integrity" << endl;
// while (c->allocated) {
// log.debug() << hex << (unsigned int)c << endl;
// c = c->next;
// }
// log.debug() << "Finished check" << endl;
log.debug() << "returning memory address " << hex << reinterpret_cast<unsigned int>(current) + sizeof(free_block_t) << endl;
lock.release();
return reinterpret_cast<void*>(reinterpret_cast<unsigned int>(current) + sizeof(free_block_t)); // Speicheranfang, nicht header
}
current = current->next;
} while (current != free_start); // Stop when arriving at the first block again
log.error() << " - More memory requested than available :(" << endl;
lock.release();
return nullptr;
}
/*****************************************************************************
* Methode: LinkedListAllocator::free *
*---------------------------------------------------------------------------*
* Beschreibung: Einen Speicherblock freigeben. *
*****************************************************************************/
void LinkedListAllocator::free(void* ptr) {
lock.acquire();
/* Hier muess Code eingefuegt werden */
// Account for header
free_block_t* block_start = reinterpret_cast<free_block_t*>(reinterpret_cast<unsigned int>(ptr) - sizeof(free_block_t));
log.debug() << "Freeing " << hex << reinterpret_cast<unsigned int>(ptr) << ", Size: " << block_start->size << endl;
if (!block_start->allocated) {
log.error() << "Block already free" << endl;
lock.release();
return;
}
// Reenable the freelist if no block was available
// This also means that no merging can be done
if (free_start == nullptr) {
free_start = block_start;
block_start->allocated = false;
block_start->next = block_start;
log.trace() << " - Enabling freelist with one block" << endl;
lock.release();
return;
}
free_block_t* next_block =
reinterpret_cast<free_block_t*>(reinterpret_cast<unsigned int>(block_start) + sizeof(free_block_t) + block_start->size);
// Find the next free block, multiple next blocks can be allocated so walk through them
free_block_t* next_free = block_start->next;
while (next_free->allocated) {
next_free = next_free->next;
}
free_block_t* previous_free = LinkedListAllocator::find_previous_block(next_free);
free_block_t* previous_free_next =
reinterpret_cast<free_block_t*>(reinterpret_cast<unsigned int>(previous_free) + sizeof(free_block_t) + previous_free->size);
// We have: [previous_free | previous_free_next | <> | block_start | next_block | <> | next_free]
// The <> spaces don't have to exist and next_block could be the same as next_free
// or previous_free_next the same as block_start
// Also next_block and previous_free_next could be allocated blocks
// If previous_free/next_free and block_start are adjacent and free, they can be merged:
// - If next_block and next_free are the same block we can merge forward
// Should result in: [previous_free | previous_free_next | <> | block_start]
// - If previous_free_next and block_start are the same block we can merge backward
// Should result in: [block_start]
// log.trace() << "Before doing any merging:" << endl;
// log.trace() << "previous_free:" << hex << (unsigned int)previous_free << "Size:" << previous_free->size << "Next:" << (unsigned int)previous_free->next << endl;
// log.trace() << "previous_free_next:" << hex << (unsigned int)previous_free_next << "Size:" << previous_free_next->size << "Next:" << (unsigned int)previous_free_next->next << endl;
// log.trace() << "block_start:" << hex << (unsigned int)block_start << "Size:" << block_start->size << "Next:" << (unsigned int)block_start->next << endl;
// log.trace() << "next_block:" << hex << (unsigned int)next_block << "Size:" << next_block->size << "Next:" << (unsigned int)next_block->next << endl;
// log.trace() << "next_free:" << hex << (unsigned int)next_free << "Size:" << next_free->size << "Next:" << (unsigned int)next_free->next << endl;
// Try to merge forward ========================================================================
if (next_block == next_free) {
log.trace() << " - Merging block forward" << endl;
// Current and next adjacent block can be merged
// [previous_free | previous_free_next | <> | block_start | next_free]
// [block_start | next_free | next_free->next] => [block_start | next_free->next]
block_start->next = next_free->next; // We make next_free disappear
if (block_start->next == next_free) {
// If next_free is the only free block it points to itself, so fix that
// [block_start | next_free], but we want to remove next_free
block_start->next = block_start;
// [block_start]
}
block_start->size = block_start->size + sizeof(free_block_t) + next_free->size;
// There shouldn't exist any other allocated blocks pointing to next_free,
// the current one should be the only one (or else I have done something wrong)
// If thats the case I should set the next pointer to the next adjacent block
// when allocating a new block
if (free_start == next_free) {
// next_free is now invalid after merge
log.trace() << " - Moving freelist start to " << hex << reinterpret_cast<unsigned int>(block_start) << endl;
free_start = block_start;
}
} else {
// Can't merge forward so size stays the same
// [previous_free | previous_free_next | <> | block_start | <> | next_free]
block_start->next = next_free;
}
// Attach new free block to freelist
// This could write into a free block, but doesn't matter
previous_free->next = block_start;
// Try to merge backward =====================================================================
if (previous_free_next == block_start) {
log.trace() << " - Merging block backward" << endl;
// Current and previous adjacent block can be merged
// [previous_free | block_start]
previous_free->next = block_start->next;
previous_free->size = previous_free->size + sizeof(free_block_t) + block_start->size;
// For pointers to block_start the same as above applies
// so I don't think I have to manage anything else here
if (free_start == block_start) {
// block_start is now invalid after merge
log.trace() << " - Moving freelist start to " << hex << reinterpret_cast<unsigned int>(previous_free) << endl;
free_start = previous_free;
}
}
// Depending on the merging this might write into the block, but doesn't matter
block_start->allocated = false;
lock.release();
}
free_block_t* LinkedListAllocator::find_previous_block(free_block_t* next_block) {
// Durchlaufe die ganze freispeicherliste bis zum Block der auf next_block zeigt
free_block_t* current = next_block;
while (current->next != next_block) {
// NOTE: This will get stuck if called on the wrong block
current = current->next;
}
// if (current == next_block) {
// kout << "LinkedListAllocator::find_previous_block returned the input block" << endl;
// }
return current;
}

View File

@ -0,0 +1,58 @@
/*****************************************************************************
* *
* L I N K E D L I S T A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung, welche den freien Speicher *
* mithilfe einer einfach verketteten Liste verwaltet. *
* *
* Autor: Michael Schoettner, HHU, 13.6.2020 *
*****************************************************************************/
#ifndef LinkedListAllocator_include__
#define LinkedListAllocator_include__
#include "Allocator.h"
#include "lib/async/SpinLock.h"
#include "kernel/log/Logger.h"
// Format eines freien Blocks, 4 + 4 + 4 Byte
typedef struct free_block {
bool allocated; // NOTE: I added this to allow easier merging of free blocks:
// When freeing an allocated block, its next-pointer can
// point to another allocated block, the next free block
// can be found by traversing the leading allocated blocks.
// We only need a way to determine when the free block is reached.
// This also means that the whole list has to be traversed
// to merge blocks. Would be faster with doubly linked list.
unsigned int size;
struct free_block* next;
} free_block_t;
class LinkedListAllocator : Allocator {
private:
// freie Bloecke werden verkettet
struct free_block* free_start = nullptr;
// Traverses the whole list forward till previous block is reached.
// This can only be called on free blocks as allocated blocks
// aren't reachable from the freelist.
static struct free_block* find_previous_block(struct free_block*);
NamedLogger log;
SpinLock lock;
public:
LinkedListAllocator(Allocator& copy) = delete; // Verhindere Kopieren
LinkedListAllocator() : log("LL-Alloc") {}
// ~LinkedListAllocator() override = default;
void init() override;
void dump_free_memory() override;
void* alloc(unsigned int req_size) override;
void free(void* ptr) override;
};
#endif

213
src/kernel/memory/Paging.cc Normal file
View File

@ -0,0 +1,213 @@
/*****************************************************************************
* *
* P A G I N G *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Rudimentaeres Paging: 1:1 Mapping fuer gesamten logischen*
* Adressraum. logische Adresse = physikalische Adresse *
* *
* Page-Directory (alle Eintraege present, read/write *
* 0. Eintrag zeigt auf eine Page-Table (4 KB Eintraege)*
* Alle restl. Eintraege sind 4 MB Seiten und verweisen *
* somit auf keine Page-Tabelle sondern direkt auf die *
* 4 MB Seite. *
* *
* Page-Table (Logische Adressen 0 - 4 MB) *
* 1. Eintrag not present, read-only *
* -> Null-Pointer abfangen *
* 2. restl. Eintraege present & read/write *
* *
* Memory-Laylout *
* *
* boot.asm *
* 0x07c0: Bootsector vom BIOS geladen *
* 0x0060: Boot-Code verschiebt sich hier hin *
* 0x9000: Setup-Code (max. 64K inkl. Stack) vom *
* Bootsector-Code geladen *
* setup.asm *
* 0x1000: System-Code (max. 512K) geladen *
* BIOS-Aufruf *
* 0x24000: Parameter fuer BIOS-Aufurf *
* 0x25000: Altes ESP sichern, vor BIOS-Aufruf *
* 0x26000: 16-Bit Code-Segment fuer BIOS-Aufurf *
* System-Code *
* 0x100000: System-Code, kopiert nach Umschalten in *
* den Protected Mode kopiert (GRUB kann nur *
* an Adressen >1M laden) *
* Globale Variablen: Direkt nach dem Code liegen die globalen *
* Variablen. *
* Paging: *
* 0x200000: Page-Directory *
* 0x201000: Page-Table *
* 0x202000: erste allozierbare Page (via Paging.cc) *
* 0x3FF000: Anfang der letzten allozierbaren Page *
* Heap: *
* 0x400000: Start-Adresse der Heap-Verwaltung *
* Ende: Letzte Adresse des phys. Speichers *
* *
* *
* Autor: Michael Schoettner, 20.12.2018 *
*****************************************************************************/
#include "kernel/memory/Paging.h"
#include "kernel/system/Globals.h"
#include "kernel/log/Logger.h"
// Bits fuer Eintraege in der Page-Table
constexpr const unsigned int PAGE_PRESENT = 0x001;
constexpr const unsigned int PAGE_WRITEABLE = 0x002;
constexpr const unsigned int PAGE_BIGSIZE = 0x080;
constexpr const unsigned int PAGE_RESERVED = 0x800; // Bit 11 ist frei fuer das OS
// Adresse des Page-Directory (benoetigt 4 KB)
constexpr const unsigned int PAGE_DIRECTORY = 0x200000;
// Adresse der Page-Table (benoetigt 4 KB)
constexpr const unsigned int PAGE_TABLE = 0x201000;
// Start- und End-Adresse der 4 KB Seiten die durch die Page-Table adressiert werden
constexpr const unsigned int FST_ALLOCABLE_PAGE = 0x202000;
constexpr const unsigned int LST_ALLOCABLE_PAGE = 0x2FF000;
/*****************************************************************************
* Funktion: pg_alloc_page *
*---------------------------------------------------------------------------*
* Beschreibung: Alloziert eine 4 KB Seite. Allozieren heisst hier *
* lediglich Setzen eines eigenen RESERVED-Bits. *
*****************************************************************************/
unsigned int* pg_alloc_page() {
unsigned int* p_page;
p_page = reinterpret_cast<unsigned int*>(PAGE_TABLE);
// 1. Eintrag ist fuer Null-Pointer-Exception reserviert
// ausserdem liegt an die Page-Table an Adresse PAGE_TABLE
// somit ist est PAGE_TABLE + 4 KB frei (bis max. 3 MB, da beginnt der Heap)
for (int i = 1; i < 1024; i++) {
p_page++;
// pruefe ob Page frei
if (((*p_page) & PAGE_RESERVED) == 0) {
*p_page = (*p_page | PAGE_RESERVED);
return reinterpret_cast<unsigned int*>(i << 12); // Address without flags (Offset 0)
}
}
return nullptr;
}
/*****************************************************************************
* Funktion: pg_write_protect_page *
*---------------------------------------------------------------------------*
* Beschreibung: Schreibschutz fuer die uebergebene Seite aktivieren. *
* Dies fuer das Debugging nuetzlich. *
*****************************************************************************/
void pg_write_protect_page(const unsigned int* p_page) {
/* hier muss Code eingefügt werden */
unsigned int* page = reinterpret_cast<unsigned int*>(PAGE_TABLE) + (reinterpret_cast<unsigned int>(p_page) >> 12); // Pagetable entry
unsigned int mask = PAGE_WRITEABLE; // fill to 32bit
*page = *page & ~mask; // set writable to 0
invalidate_tlb_entry(p_page);
}
/*****************************************************************************
* Funktion: pg_notpresent_page *
*---------------------------------------------------------------------------*
* Beschreibung: Seite als ausgelagert markieren. Nur fuer Testzwecke. *
*****************************************************************************/
void pg_notpresent_page(const unsigned int* p_page) {
/* hier muss Code eingefügt werden */
unsigned int* page = reinterpret_cast<unsigned int*>(PAGE_TABLE) + (reinterpret_cast<unsigned int>(p_page) >> 12); // Pagetable entry
unsigned int mask = PAGE_PRESENT;
*page = *page & ~mask; // set present to 0
invalidate_tlb_entry(p_page);
}
/*****************************************************************************
* Funktion: pg_free_page *
*---------------------------------------------------------------------------*
* Beschreibung: Gibt eine 4 KB Seite frei. Es wird hierbei das RESERVED- *
* Bit geloescht. *
*****************************************************************************/
void pg_free_page(unsigned int* p_page) {
unsigned int idx = reinterpret_cast<unsigned int>(p_page) >> 12;
// ausserhalb Page ?
if (idx < 1 || idx > 1023) {
return;
}
// Eintrag einlesen und aendern (PAGE_WRITEABLE loeschen)
p_page = reinterpret_cast<unsigned int*>(PAGE_TABLE);
p_page += idx;
*p_page = ((idx << 12) | PAGE_WRITEABLE | PAGE_PRESENT);
}
/*****************************************************************************
* Funktion: pg_init *
*---------------------------------------------------------------------------*
* Beschreibung: Page-Tables einrichten und Paging mithilfe von *
* startup.asm aktivieren. *
*****************************************************************************/
void pg_init() {
unsigned int i;
unsigned int* p_pdir; // Zeiger auf Page-Directory
unsigned int* p_page; // Zeiger auf einzige Page-Table fuer 4 KB Pages
unsigned int num_pages; // Anzahl 4 MB Pages die phys. Adressraum umfassen
// wie viele 4 MB Seiten sollen als 'Present' angelegt werden,
// sodass genau der physikalische Adressraum abgedeckt ist?
num_pages = total_mem / (4096 * 1024);
Logger::instance() << INFO << "pg_init: " << total_mem << endl;
Logger::instance() << INFO << " total_mem: " << total_mem << endl;
Logger::instance() << INFO << " #pages: " << total_mem / (4096 * 1024) << endl;
//
// Aufbau des Page-Directory
//
// Eintrag 0: Zeiger auf 4 KB Page-Table
p_pdir = reinterpret_cast<unsigned int*>(PAGE_DIRECTORY);
*p_pdir = PAGE_TABLE | PAGE_WRITEABLE | PAGE_PRESENT;
// Eintraege 1-1023: Direktes Mapping (1:1) auf 4 MB Pages (ohne Page-Table)
for (i = 1; i < 1024; i++) {
p_pdir++;
if (i > num_pages) {
*p_pdir = ((i << 22) | PAGE_BIGSIZE);
} else {
*p_pdir = ((i << 22) | PAGE_BIGSIZE | PAGE_WRITEABLE | PAGE_PRESENT);
}
}
//
// 1. Page-Table
//
p_page = reinterpret_cast<unsigned int*>(PAGE_TABLE);
// ersten Eintrag loeschen -> not present, write protected -> Null-Pointer abfangen
*p_page = 0;
// Eintraege 1-1023: Direktes Mapping (1:1) auf 4 KB page frames
for (i = 1; i < 1024; i++) {
p_page++;
// Seiten unter FST_ALLOCABLE_PAGE reservieren, damit diese nicht
// alloziert werden und das System kaputt geht
if ((i << 12) >= FST_ALLOCABLE_PAGE) {
*p_page = ((i << 12) | PAGE_WRITEABLE | PAGE_PRESENT);
} else {
*p_page = ((i << 12) | PAGE_WRITEABLE | PAGE_PRESENT | PAGE_RESERVED);
}
}
// Paging aktivieren (in startup.asm)
paging_on(reinterpret_cast<unsigned int*>(PAGE_DIRECTORY));
}

View File

@ -0,0 +1,76 @@
/*****************************************************************************
* *
* P A G I N G *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Rudimentaeres Paging: 1:1 Mapping fuer gesamten logischen*
* Adressraum. logische Adresse = physikalische Adresse *
* *
* Page-Directory (alle Eintraege present, read/write *
* 0. Eintrag zeigt auf eine Page-Table (4 KB Eintraege)*
* Alle restl. Eintraege sind 4 MB Seiten und verweisen *
* somit auf keine Page-Tabelle sondern direkt auf die *
* 4 MB Seite. *
* *
* Page-Table (Logische Adressen 0 - 4 MB) *
* 1. Eintrag not present, read-only *
* -> Null-Pointer abfangen *
* 2. restl. Eintraege present & read/write *
* *
* Memory-Laylout *
* *
* boot.asm *
* 0x07c0: Bootsector vom BIOS geladen *
* 0x0060: Boot-Code verschiebt sich hier hin *
* 0x9000: Setup-Code (max. 64K inkl. Stack) vom *
* Bootsector-Code geladen *
* setup.asm *
* 0x1000: System-Code (max. 512K) geladen *
* BIOS-Aufruf *
* 0x24000: Parameter fuer BIOS-Aufurf *
* 0x25000: Altes ESP sichern, vor BIOS-Aufruf *
* 0x26000: 16-Bit Code-Segment fuer BIOS-Aufurf *
* System-Code *
* 0x100000: System-Code, kopiert nach Umschalten in *
* den Protected Mode kopiert (GRUB kann nur *
* an Adressen >1M laden) *
* Globale Variablen: Direkt nach dem Code liegen die globalen *
* Variablen. *
* Paging: *
* 0x200000: Page-Directory *
* 0x201000: Page-Table *
* 0x202000: erste allozierbare Page (via Paging.cc) *
* 0x3FF000: letzte allozierbare Page *
* Heap: *
* 0x400000: Start-Adresse der Heap-Verwaltung *
* Ende: Letzte Adresse des phys. Speichers *
* *
* *
* Autor: Michael Schoettner, 2.2.2017 *
*****************************************************************************/
#ifndef Paging_include__
#define Paging_include__
// Externe Funktionen in startup.asm
extern "C" {
void paging_on(unsigned int* p_pdir); // Paging einschalten
void invalidate_tlb_entry(const unsigned int* ptr); // Page in TLB invalid.
}
// ativiert paging
extern void pg_init();
// alloziert eine 4 KB Page
extern unsigned int* pg_alloc_page();
// Schreibschutz auf Seite setzen -> fuer debugging nuetzlich
extern void pg_write_protect_page(const unsigned int* p_page);
// Present Bit loeschen
extern void pg_notpresent_page(const unsigned int* p_page);
// gibt eine 4 KB Page frei
extern void pg_free_page(unsigned int* p_page);
#endif

View File

@ -0,0 +1,163 @@
#include "TreeAllocator.h"
#include "kernel/system/Globals.h"
void TreeAllocator::init() {
free_start = reinterpret_cast<tree_block_t*>(heap_start);
free_start->allocated = false;
free_start->left = nullptr;
free_start->right = nullptr;
free_start->parent = nullptr;
free_start->red = false; // The root is always black
free_start->next = reinterpret_cast<list_block_t*>(free_start);
free_start->previous = reinterpret_cast<list_block_t*>(free_start);
log.info() << "Initialized Tree Allocator" << endl;
}
void TreeAllocator::dump_free_memory() {
kout << "Free Memory:" << endl;
list_block_t* current = reinterpret_cast<list_block_t*>(heap_start);
do {
if (!current->allocated) {
kout << " - Free Block at " << reinterpret_cast<unsigned int>(current) << ", Size: "
<< reinterpret_cast<unsigned int>(current->next) - reinterpret_cast<unsigned int>(current)
<< endl;
}
current = current->next;
} while (reinterpret_cast<unsigned int>(current) != heap_start);
}
void* TreeAllocator::alloc(unsigned int req_size) {
log.debug() << "Requested " << dec << req_size << " Bytes" << endl;
// Round to word borders + tree_block size
unsigned int rreq_size = req_size;
if (rreq_size < sizeof(tree_block_t) - sizeof(list_block_t)) {
// the list_block_t is part of every block, but when freeing
// memory we need enough space to store the rbt metadata
rreq_size = sizeof(tree_block_t) - sizeof(list_block_t);
log.trace() << " - Increased block size for rbt metadata" << endl;
}
unsigned int req_size_diff = (BASIC_ALIGN - rreq_size % BASIC_ALIGN) % BASIC_ALIGN;
rreq_size = rreq_size + req_size_diff;
if (req_size_diff > 0) {
log.trace() << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl;
}
// Finds smallest block that is large enough
tree_block_t* best_fit = rbt_search_bestfit(rreq_size);
if (best_fit == nullptr) {
log.error() << " - No block found" << endl;
return nullptr;
}
if (best_fit->allocated) {
// Something went really wrong
log.error() << " - Block already allocated :(" << endl;
return nullptr;
}
best_fit->allocated = true;
unsigned int size = get_size(best_fit);
log.trace() << " - Found best-fit: " << hex << reinterpret_cast<unsigned int>(best_fit) << endl;
// HACK: I didn't want to handle situations with only one block (where the tree root would
// get removed), so I make sure there are always at least 2 blocks by inserting a dummy
// block. This is not fast at all but it was fast to code...
// I should change this so it only happens when only one block exists in the freelist
tree_block_t dummy;
dummy.allocated = false;
rbt_insert(&dummy); // I use the address of the stack allocated struct because it is
// removed before exiting the function
rbt_remove(best_fit); // BUG: Can trigger bluescreen
if (size > HEAP_MIN_FREE_BLOCK_SIZE + rreq_size + sizeof(list_block_t)) {
// Block can be cut
log.trace() << " - Allocating " << dec << rreq_size << " Bytes with cutting" << endl;
// [best_fit_start | sizeof(list_block_t) | rreq_size | new_block_start]
tree_block_t* new_block
= reinterpret_cast<tree_block_t*>(reinterpret_cast<char*>(best_fit) + sizeof(list_block_t) + rreq_size);
new_block->allocated = false;
dll_insert(best_fit, new_block);
rbt_insert(new_block);
} else {
// Don't cut block
// The block is already correctly positioned in the linked list so we only
// need to remove it from the freelist, which is done for both cases
log.trace() << " - Allocating " << dec << rreq_size << " Bytes without cutting" << endl;
}
// HACK: Remove the dummy element
rbt_remove(&dummy);
log.trace() << " - Returned address " << hex
<< reinterpret_cast<unsigned int>(reinterpret_cast<char*>(best_fit) + sizeof(list_block_t))
<< endl;
return reinterpret_cast<void*>(reinterpret_cast<char*>(best_fit) + sizeof(list_block_t));
}
void TreeAllocator::free(void* ptr) {
log.info() << "Freeing " << hex << reinterpret_cast<unsigned int>(ptr) << endl;
list_block_t* block = reinterpret_cast<list_block_t*>(reinterpret_cast<char*>(ptr) - sizeof(list_block_t));
if (!block->allocated) {
// Block already free
return;
}
block->allocated = false; // If the block is merged backwards afterwards this is unnecessary
list_block_t* previous = block->previous;
list_block_t* next = block->next;
if (next->allocated && previous->allocated) {
// No merge
rbt_insert(reinterpret_cast<tree_block_t*>(block));
return;
}
// HACK: Same as when allocating
tree_block_t dummy;
dummy.allocated = false;
rbt_insert(&dummy); // I use the address of the stack allocated struct because it is
if (!next->allocated) {
// Merge forward
log.trace() << " - Merging forward" << endl;
// Remove the next block from all lists as it is now part of our freed block
dll_remove(next);
rbt_remove(reinterpret_cast<tree_block_t*>(next)); // BUG: Bluescreen if next is the only block in the freelist
if (previous->allocated) {
// Don't insert if removed later because of backward merge
rbt_insert(reinterpret_cast<tree_block_t*>(block));
}
}
if (!previous->allocated) {
// Merge backward
log.trace() << " - Merging backward" << endl;
// Remove the current block from all lists as it is now part of the previous block
// It doesn't have to be removed from rbt as it wasn't in there as it was allocated before
dll_remove(block);
rbt_remove(reinterpret_cast<tree_block_t*>(previous));
rbt_insert(reinterpret_cast<tree_block_t*>(previous)); // Reinsert with new size
}
// HACK: Same as when allocating
rbt_remove(&dummy);
}
unsigned int TreeAllocator::get_size(list_block_t* block) const {
if (block->next == block) {
// Only one block exists
return heap_end - (reinterpret_cast<unsigned int>(block) + sizeof(list_block_t));
}
if (reinterpret_cast<unsigned int>(block->next) > reinterpret_cast<unsigned int>(block)) {
// Next block is placed later in memory
return reinterpret_cast<unsigned int>(block->next) - (reinterpret_cast<unsigned int>(block) + sizeof(list_block_t));
}
// Next block is placed earlier in memory which means block is at memory end
return reinterpret_cast<unsigned int>(heap_end) - (reinterpret_cast<unsigned int>(block) + sizeof(list_block_t));
}

View File

@ -0,0 +1,81 @@
#ifndef TreeAllocator_include__
#define TreeAllocator_include__
#include "Allocator.h"
#include "kernel/log/Logger.h"
// I can't imagine that this is fast with all the tree logic?
typedef struct list_block {
// Doubly linked list for every block
bool allocated;
struct list_block* next;
struct list_block* previous;
} list_block_t;
// The free blocks are organized in a red-black tree to enable fast insertion with best-fit strategy.
// To allow fast merging of freed blocks every block is part of a doubly linked list.
// Because the red-black tree only contains the free blocks, the memory overhead comes
// down to 4 + 4 + 4 Bytes for the allocated flag, next and previous pointers.
// The size can be calculated by using the next pointer so it doesn't have to be stored.
typedef struct tree_block {
// Doubly linked list for every block
// Locate this at the beginning so we can just cast to allocated_block_t and overwrite the rbt data
bool allocated;
struct list_block* next;
struct list_block* previous;
// RB tree for free blocks
struct tree_block* left;
struct tree_block* right;
struct tree_block* parent;
bool red; // RB tree node color
} tree_block_t;
class TreeAllocator : Allocator {
private:
// Root of the rbt
tree_block_t* free_start;
NamedLogger log;
// Returns the size of the usable memory of a block
unsigned int get_size(list_block_t* block) const;
unsigned int get_size(tree_block_t* block) const { return get_size(reinterpret_cast<list_block_t*>(block)); }
void dump_free_memory(tree_block_t* node);
// NOTE: Would be nice to have this stuff somewhere else for general use (scheduling?),
// makes no sense to have this as members. I'll move it later
void rbt_rot_l(tree_block_t* x);
void rbt_rot_r(tree_block_t* x);
void rbt_transplant(tree_block_t* a, tree_block_t* b);
tree_block_t* rbt_minimum(tree_block_t* node);
void rbt_insert(tree_block_t* node);
void rbt_fix_insert(tree_block_t* k);
void rbt_remove(tree_block_t* z);
void rbt_fix_remove(tree_block_t* x);
tree_block_t* rbt_search_bestfit(tree_block_t* node, unsigned int req_size);
tree_block_t* rbt_search_bestfit(unsigned int req_size) { return rbt_search_bestfit(free_start, req_size); }
void dll_insert(list_block_t* previous, list_block_t* node);
void dll_insert(tree_block_t* previous, tree_block_t* node) {
dll_insert(reinterpret_cast<list_block_t*>(previous), reinterpret_cast<list_block_t*>(node));
}
void dll_remove(list_block_t* node);
public:
TreeAllocator(Allocator& copy) = delete; // Verhindere Kopieren
TreeAllocator() : log("RBT-Alloc") {};
// ~TreeAllocator() override = default;
void init() override;
void dump_free_memory() override;
void* alloc(unsigned int req_size) override;
void free(void* ptr) override;
};
#endif

View File

@ -0,0 +1,302 @@
#include "TreeAllocator.h"
// RBT code taken from https://github.com/Bibeknam/algorithmtutorprograms
// START copy from algorithmtutorprograms
void TreeAllocator::rbt_transplant(tree_block_t* a, tree_block_t* b) {
if (a->parent == nullptr) {
free_start = b;
} else if (a == a->parent->left) {
a->parent->left = b;
} else {
a->parent->right = b;
}
b->parent = a->parent;
}
// insert the key to the tree in its appropriate position
// and fix the tree
void TreeAllocator::rbt_insert(tree_block_t* node) {
// Ordinary Binary Search Insertion
node->parent = nullptr;
node->left = nullptr;
node->right = nullptr;
node->red = true; // new node must be red
tree_block_t* y = nullptr;
tree_block_t* x = free_start;
while (x != nullptr) {
y = x;
if (get_size(node) < get_size(x)) {
x = x->left;
} else {
x = x->right;
}
}
// y is parent of x
node->parent = y;
if (y == nullptr) {
free_start = node;
} else if (get_size(node) < get_size(y)) {
y->left = node;
} else {
y->right = node;
}
// if new node is a root node, simply return
if (node->parent == nullptr) {
node->red = false;
return;
}
// if the grandparent is null, simply return
if (node->parent->parent == nullptr) {
return;
}
// Fix the tree
rbt_fix_insert(node);
}
// fix the red-black tree
void TreeAllocator::rbt_fix_insert(tree_block_t* k) {
tree_block_t* u;
while (k->parent->red) {
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left; // uncle
if (u->red) {
// case 3.1
u->red = false;
k->parent->red = false;
k->parent->parent->red = true;
k = k->parent->parent;
} else {
if (k == k->parent->left) {
// case 3.2.2
k = k->parent;
rbt_rot_r(k);
}
// case 3.2.1
k->parent->red = false;
k->parent->parent->red = true;
rbt_rot_l(k->parent->parent);
}
} else {
u = k->parent->parent->right; // uncle
if (u->red) {
// mirror case 3.1
u->red = false;
k->parent->red = false;
k->parent->parent->red = true;
k = k->parent->parent;
} else {
if (k == k->parent->right) {
// mirror case 3.2.2
k = k->parent;
rbt_rot_l(k);
}
// mirror case 3.2.1
k->parent->red = false;
k->parent->parent->red = true;
rbt_rot_r(k->parent->parent);
}
}
if (k == free_start) {
break;
}
}
free_start->red = false;
}
// rotate left at node x
void TreeAllocator::rbt_rot_l(tree_block_t* x) {
tree_block_t* y = x->right;
x->right = y->left;
if (y->left != nullptr) {
y->left->parent = x;
}
y->parent = x->parent;
if (x->parent == nullptr) {
free_start = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
}
y->left = x;
x->parent = y;
}
// rotate right at node x
void TreeAllocator::rbt_rot_r(tree_block_t* x) {
tree_block_t* y = x->left;
x->left = y->right;
if (y->right != nullptr) {
y->right->parent = x;
}
y->parent = x->parent;
if (x->parent == nullptr) {
free_start = y;
} else if (x == x->parent->right) {
x->parent->right = y;
} else {
x->parent->left = y;
}
y->right = x;
x->parent = y;
}
// find the node with the minimum key
tree_block_t* TreeAllocator::rbt_minimum(tree_block_t* node) {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
void TreeAllocator::rbt_remove(tree_block_t* z) {
tree_block_t* x;
tree_block_t* y;
y = z;
bool y_original_red = y->red;
if (z->left == nullptr) {
x = z->right;
rbt_transplant(z, z->right);
} else if (z->right == nullptr) {
x = z->left;
rbt_transplant(z, z->left);
} else {
y = rbt_minimum(z->right);
y_original_red = y->red;
x = y->right;
if (y->parent == z) {
x->parent = y;
} else {
rbt_transplant(y, y->right);
y->right = z->right;
y->right->parent = y;
}
rbt_transplant(z, y);
y->left = z->left;
y->left->parent = y;
y->red = z->red;
}
if (!y_original_red) {
rbt_fix_remove(x);
}
}
// fix the rb tree modified by the delete operation
void TreeAllocator::rbt_fix_remove(tree_block_t* x) {
tree_block_t* s;
while (x != free_start && !x->red) {
if (x == x->parent->left) {
s = x->parent->right;
if (s->red) {
// case 3.1
s->red = false;
x->parent->red = true;
rbt_rot_l(x->parent);
s = x->parent->right;
}
if (!s->left->red && !s->right->red) {
// case 3.2
s->red = true;
x = x->parent;
} else {
if (!s->right->red) {
// case 3.3
s->left->red = false;
s->red = true;
rbt_rot_r(s);
s = x->parent->right;
}
// case 3.4
s->red = x->parent->red;
x->parent->red = false;
s->right->red = false;
rbt_rot_l(x->parent);
x = free_start;
}
} else {
s = x->parent->left;
if (s->red) {
// case 3.1
s->red = false;
x->parent->red = true;
rbt_rot_r(x->parent);
s = x->parent->left;
}
if (!s->right->red) {
// case 3.2
s->red = true;
x = x->parent;
} else {
if (!s->left->red) {
// case 3.3
s->right->red = false;
s->red = true;
rbt_rot_l(s);
s = x->parent->left;
}
// case 3.4
s->red = x->parent->red;
x->parent->red = false;
s->left->red = false;
rbt_rot_r(x->parent);
x = free_start;
}
}
}
x->red = false;
}
// END copy from algorithmtutorprograms
// This is recursive and depends on luck
tree_block_t* TreeAllocator::rbt_search_bestfit(tree_block_t* node, unsigned int req_size) {
if (node == nullptr) {
return nullptr;
}
if (req_size < get_size(node)) {
if (node->left != nullptr && get_size(node->left) >= req_size) {
return rbt_search_bestfit(node->left, req_size);
}
return node;
}
if (req_size > get_size(node)) {
if (node->right != nullptr && get_size(node->right) >= req_size) {
return rbt_search_bestfit(node->right, req_size);
}
// Block doesn't fit
return nullptr;
}
// Perfect fit
return node;
}
// DLL code
void TreeAllocator::dll_insert(list_block_t* previous, list_block_t* node) {
previous->next->previous = node;
node->next = previous->next;
node->previous = previous;
previous->next = node;
}
void TreeAllocator::dll_remove(list_block_t* node) {
node->previous->next = node->next;
node->next->previous = node->previous;
}

View File

@ -0,0 +1,38 @@
/*****************************************************************************
* *
* I D L E T H R E A D *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Wird nur aktiviert, wenn kein Thread arbeiten moechte. *
* *
* Autor: Michael, Schoettner, HHU, 13.8.2016 *
*****************************************************************************/
#ifndef IdleThread_include__
#define IdleThread_include__
#include "kernel/system/Globals.h"
#include "Thread.h"
class IdleThread : public Thread {
public:
IdleThread(const Thread& copy) = delete; // Verhindere Kopieren
IdleThread() : Thread("IdleThread") {}
[[noreturn]] void run() override {
// Idle-Thread läuft, ab jetzt ist der Scheduler fertig initialisiert
log.info() << "IdleThread enabled preemption" << endl;
scheduler.enable_preemption(tid);
if (!scheduler.preemption_enabled()) {
log.error() << "Preemption disabled" << endl;
}
while (true) {
// kout << "Idle!" << endl;
scheduler.yield();
}
}
};
#endif

View File

@ -0,0 +1,341 @@
/*****************************************************************************
* *
* S C H E D U L E R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines einfachen Zeitscheiben-Schedulers. *
* Rechenbereite Threads werden in 'readQueue' verwaltet. *
* *
* Der Scheduler wird mit 'schedule' gestartet. Neue Threads*
* können mit 'ready' hinzugefügt werden. Ein Thread muss *
* die CPU::freiwillig mit 'yield' abgeben, damit andere auch*
* rechnen koennen. Ein Thread kann sich selbst mit 'exit' *
* terminieren. Ein Thread kann einen anderen Thread mit *
* 'kill' beenden. Ein erzwungener Threadwechsel erfolgt *
* mit der Funktion 'preempt', welche von der Timer-ISR *
* aufgerufen wird. *
* *
* Zusaetzlich gibt es nun fuer die Semaphore zwei neue *
* Funktionen 'block' und 'deblock'. *
* *
* Autor: Michael, Schoettner, HHU, 23.11.2018 *
*****************************************************************************/
#include "Scheduler.h"
#include "IdleThread.h"
#include <utility>
constexpr const bool INSANE_TRACE = false;
/*****************************************************************************
* Methode: Dispatcher::dispatch *
*---------------------------------------------------------------------------*
* Beschreibung: Auf den active thread wechseln. *
* *
* Parameter: *
* next Thread der die CPU::erhalten soll. *
*****************************************************************************/
void Scheduler::start(bse::vector<bse::unique_ptr<Thread>>::iterator next) {
active = next;
if (active >= ready_queue.end()) {
active = ready_queue.begin();
log.debug() << "Scheduler::start started different thread than passed" << endl;
}
if constexpr (INSANE_TRACE) {
log.trace() << "Starting Thread with id: " << dec << (*active)->tid << endl;
}
(*active)->start(); // First dereference the Iterator, then the unique_ptr to get Thread
}
void Scheduler::switch_to(Thread* prev_raw, bse::vector<bse::unique_ptr<Thread>>::iterator next) {
active = next;
if (active >= ready_queue.end()) {
active = ready_queue.begin();
// log.debug() << "Scheduler::switch_to started different thread than passed" << endl;
}
if constexpr (INSANE_TRACE) {
log.trace() << "Switching to Thread with id: " << dec << (*active)->tid << endl;
}
prev_raw->switchTo(**active);
}
/*****************************************************************************
* Methode: Scheduler::schedule *
*---------------------------------------------------------------------------*
* Beschreibung: Scheduler starten. Wird nur einmalig aus main.cc gerufen.*
*****************************************************************************/
void Scheduler::schedule() {
/* hier muss Code eingefuegt werden */
// We need to start the idle thread first as this one sets the scheduler to initialized
// and enables preemption.
// Otherwise preemption will be blocked and nothing will happen if the first threads
// run() function is blocking
ready_queue.push_back(bse::make_unique<IdleThread>());
log.info() << "Starting scheduling: starting thread with id: " << dec << (*(ready_queue.end() - 1))->tid << endl;
start(ready_queue.end() - 1);
}
/*****************************************************************************
* Methode: Scheduler::ready *
*---------------------------------------------------------------------------*
* Beschreibung: Thread in readyQueue eintragen. *
*****************************************************************************/
void Scheduler::ready(bse::unique_ptr<Thread>&& thread) {
CPU::disable_int();
log.debug() << "Adding to ready_queue, ID: " << dec << thread->tid << endl;
ready_queue.push_back(std::move(thread));
CPU::enable_int();
}
/*****************************************************************************
* Methode: Scheduler::exit *
*---------------------------------------------------------------------------*
* Beschreibung: Thread ist fertig und terminiert sich selbst. Hier muss *
* nur auf den naechsten Thread mithilfe des Dispatchers *
* umgeschaltet werden. Der aktuell laufende Thread ist *
* nicht in der readyQueue. *
*****************************************************************************/
void Scheduler::exit() {
/* hier muss Code eingefuegt werden */
// Thread-Wechsel durch PIT verhindern
CPU::disable_int();
if (ready_queue.size() == 1) {
log.error() << "Can't exit last thread, active ID: " << dec << (*active)->tid << endl;
CPU::enable_int();
return;
}
log.debug() << "Exiting thread, ID: " << dec << (*active)->tid << endl;
start(ready_queue.erase(active)); // erase returns the next iterator after the erased element
// cannot use switch_to here as the previous thread no longer
// exists (was deleted by erase)
// Interrupts werden in Thread_switch in Thread.asm wieder zugelassen
// dispatch kehr nicht zurueck
}
/*****************************************************************************
* Methode: Scheduler::kill *
*---------------------------------------------------------------------------*
* Beschreibung: Thread mit 'Gewalt' terminieren. Er wird aus der *
* readyQueue ausgetragen und wird dann nicht mehr aufge- *
* rufen. Der Aufrufer dieser Methode muss ein anderer *
* Thread sein. *
* *
* Parameter: *
* that Zu terminierender Thread *
*****************************************************************************/
void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
CPU::disable_int();
unsigned int prev_tid = (*active)->tid;
// Block queue, can always kill
for (bse::vector<bse::unique_ptr<Thread>>::iterator it = block_queue.begin(); it != block_queue.end(); ++it) {
if ((*it)->tid == tid) {
// Found thread to kill
if (ptr != nullptr) {
// Move old thread out of queue to return it
unsigned int pos = bse::distance(block_queue.begin(), it);
*ptr = std::move(block_queue[pos]); // Return the killed thread
}
// Just erase from queue, do not need to switch
block_queue.erase(it);
log.info() << "Killed thread from block_queue with id: " << tid << endl;
CPU::enable_int();
return;
}
}
// Ready queue, can't kill last one
if (ready_queue.size() == 1) {
log.error() << "Kill: Can't kill last thread in ready_queue with id: " << tid << endl;
CPU::enable_int();
return;
}
for (bse::vector<bse::unique_ptr<Thread>>::iterator it = ready_queue.begin(); it != ready_queue.end(); ++it) {
if ((*it)->tid == tid) {
// Found thread to kill
if (ptr != nullptr) {
// Move old thread out of queue to return it
unsigned int pos = bse::distance(ready_queue.begin(), it);
*ptr = std::move(ready_queue[pos]); // Return the killed thread
}
if (tid == prev_tid) {
// If we killed the active thread we need to switch to another one
log.info() << "Killed active thread from ready_queue with id: " << tid << endl;
// Switch to current active after old active was removed
start(ready_queue.erase(it));
}
// Just erase from queue, do not need to switch
ready_queue.erase(it);
log.info() << "Killed thread from ready_queue with id: " << tid << endl;
CPU::enable_int();
return;
}
}
log.error() << "Kill: Couldn't find thread with id: " << tid << " in ready- or block-queue" << endl;
log.error() << "Mabe it already exited itself?" << endl;
CPU::enable_int();
}
// TODO: Can't retrive the thread right now because it's not clear when it's finished,
// maybe introduce a exited_queue and get it from there
void Scheduler::nice_kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
CPU::disable_int();
for (bse::unique_ptr<Thread>& thread : block_queue) {
if (thread->tid == tid) {
thread->suicide();
log.info() << "Nice killed thread in block_queue with id: " << tid << endl;
deblock(tid);
CPU::enable_int();
return;
}
}
for (bse::unique_ptr<Thread>& thread : ready_queue) {
if (thread->tid == tid) {
thread->suicide();
log.info() << "Nice killed thread in ready_queue with id: " << tid << endl;
CPU::enable_int();
return;
}
}
log.error() << "Can't nice kill thread (not found) with id: " << tid << endl;
log.error() << "Mabe it already exited itself?" << endl;
CPU::enable_int();
}
/*****************************************************************************
* Methode: Scheduler::yield *
*---------------------------------------------------------------------------*
* Beschreibung: CPU::freiwillig abgeben und Auswahl des naechsten Threads.*
* Naechsten Thread aus der readyQueue holen, den aktuellen *
* in die readyQueue wieder eintragen. Das Umschalten soll *
* mithilfe des Dispatchers erfolgen. *
* *
* Achtung: Falls nur der Idle-Thread läuft, so ist die *
* readyQueue leer. *
*****************************************************************************/
void Scheduler::yield() {
/* hier muss Code eingefuegt werden */
// Thread-Wechsel durch PIT verhindern
CPU::disable_int();
if (ready_queue.size() == 1) {
if constexpr (INSANE_TRACE) {
log.trace() << "Skipping yield as no thread is waiting, active ID: " << dec << (*active)->tid << endl;
}
CPU::enable_int();
return;
}
if constexpr (INSANE_TRACE) {
log.trace() << "Yielding, ID: " << dec << (*active)->tid << endl;
}
switch_to((*active).get(), active + 1); // prev_raw is valid since no thread was killed/deleted
}
/*****************************************************************************
* Methode: Scheduler::preempt *
*---------------------------------------------------------------------------*
* Beschreibung: Diese Funktion wird aus der ISR des PITs aufgerufen und *
* schaltet auf den naechsten Thread um, sofern einer vor- *
* handen ist. *
*****************************************************************************/
void Scheduler::preempt() {
/* Hier muss Code eingefuegt werden */
CPU::disable_int();
yield();
}
/*****************************************************************************
* Methode: Scheduler::block *
*---------------------------------------------------------------------------*
* Beschreibung: Aufrufer ist blockiert. Es soll auf den naechsten Thread *
* umgeschaltet werden. Der Aufrufer soll nicht in die *
* readyQueue eingefuegt werden und wird extern verwaltet. *
* Wird bei uns nur fuer Semaphore verwendet. Jede Semaphore*
* hat eine Warteschlange wo der Thread dann verwaltet wird.*
* Die Methode kehrt nicht zurueck, sondern schaltet um. *
*****************************************************************************/
void Scheduler::block() {
/* hier muss Code eingefuegt werden */
CPU::disable_int();
if (ready_queue.size() == 1) {
log.error() << "Can't block last thread, active ID: " << dec << (*active)->tid << endl;
CPU::enable_int();
return;
}
Thread* prev_raw = (*active).get();
std::size_t pos = bse::distance(ready_queue.begin(), active);
block_queue.push_back(std::move(ready_queue[pos]));
if constexpr (INSANE_TRACE) {
log.trace() << "Blocked thread with id: " << prev_raw->tid << endl;
}
switch_to(prev_raw, ready_queue.erase(active)); // prev_raw is valid as thread was moved before vector erase
}
/*****************************************************************************
* Methode: Scheduler::deblock *
*---------------------------------------------------------------------------*
* Beschreibung: Thread 'that' deblockieren. 'that' wird nur in die *
* readyQueue eingefuegt und dann zurueckgekehrt. In der *
* einfachsten Form entspricht diese Funktion exakt 'ready' *
* Man koennte alternativ aber den deblockierten Thread auch*
* am Anfang der readyQueue einfuegen, um ihn zu beorzugen. *
* *
* Parameter: that: Thread der deblockiert werden soll. *
*****************************************************************************/
void Scheduler::deblock(unsigned int tid) {
/* hier muss Code eingefuegt werden */
CPU::disable_int();
for (bse::vector<bse::unique_ptr<Thread>>::iterator it = block_queue.begin(); it != block_queue.end(); ++it) {
if ((*it)->tid == tid) {
// Found thread with correct tid
std::size_t pos = bse::distance(block_queue.begin(), it);
ready_queue.insert(active + 1, std::move(block_queue[pos])); // We insert the thread after the active
// thread to prefer deblocked threads
block_queue.erase(it);
if constexpr (INSANE_TRACE) {
log.trace() << "Deblocked thread with id: " << tid << endl;
}
CPU::enable_int();
return;
}
}
log.error() << "Couldn't deblock thread with id: " << tid << endl;
CPU::enable_int();
}

View File

@ -0,0 +1,109 @@
/*****************************************************************************
* *
* S C H E D U L E R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines einfachen Zeitscheiben-Schedulers. *
* Rechenbereite Threads werden in 'readQueue' verwaltet. *
* *
* Autor: Michael, Schoettner, HHU, 22.8.2016 *
*****************************************************************************/
#ifndef Scheduler_include__
#define Scheduler_include__
#include "Thread.h"
#include "lib/mem/UniquePointer.h"
#include "kernel/log/Logger.h"
#include "lib/util/Vector.h"
class Scheduler {
private:
NamedLogger log;
bse::vector<bse::unique_ptr<Thread>> ready_queue;
bse::vector<bse::unique_ptr<Thread>> block_queue;
// NOTE: It makes sense to keep track of the active thread through this as it makes handling the
// unique_ptr easier and reduces the copying in the vector when cycling through the threads
bse::vector<bse::unique_ptr<Thread>>::iterator active = nullptr;
// Scheduler wird evt. von einer Unterbrechung vom Zeitgeber gerufen,
// bevor er initialisiert wurde
unsigned int idle_tid = 0U;
// Roughly the old dispatcher functionality
void start(bse::vector<bse::unique_ptr<Thread>>::iterator next); // Start next without prev
void switch_to(Thread* prev_raw, bse::vector<bse::unique_ptr<Thread>>::iterator next); // Switch from prev to next
// Kann nur vom Idle-Thread aufgerufen werden (erster Thread der vom Scheduler gestartet wird)
void enable_preemption(unsigned int tid) { idle_tid = tid; }
friend class IdleThread;
void ready(bse::unique_ptr<Thread>&& thread);
public:
Scheduler(const Scheduler& copy) = delete; // Verhindere Kopieren
Scheduler() : log("SCHED"), ready_queue(true), block_queue(true) {} // lazy queues, wait for allocator
// The scheduler has to init the queues explicitly after the allocator is available
void init() {
ready_queue.reserve();
block_queue.reserve();
}
unsigned int get_active() const {
return (*active)->tid;
}
// Scheduler initialisiert?
// Zeitgeber-Unterbrechung kommt evt. bevor der Scheduler fertig
// intiialisiert wurde!
bool preemption_enabled() const { return idle_tid != 0U; }
// Scheduler starten
void schedule();
// Helper that directly constructs the thread, then readys it
template<typename T, typename... Args>
unsigned int ready(Args... args) {
bse::unique_ptr<Thread> thread = bse::make_unique<T>(std::forward<Args>(args)...);
unsigned int tid = thread->tid;
ready(std::move(thread));
return tid;
}
// Thread terminiert sich selbst
// NOTE: When a thread exits itself it will disappear...
// Maybe put exited threads in an exited queue?
// Then they would have to be acquired from there to exit...
void exit(); // Returns on error because we don't have exceptions
// Thread mit 'Gewalt' terminieren
void kill(unsigned int tid, bse::unique_ptr<Thread>* ptr);
void kill(unsigned int tid) { kill(tid, nullptr); }
// Asks thread to exit
// NOTE: I had many problems with killing threads that were stuck in some semaphore
// or were involved in any locking mechanisms, so with this a thread can make sure
// to "set things right" before exiting itself (but could also be ignored)
void nice_kill(unsigned int tid, bse::unique_ptr<Thread>* ptr);
void nice_kill(unsigned int tid) { nice_kill(tid, nullptr); }
// CPU freiwillig abgeben und Auswahl des naechsten Threads
void yield(); // Returns when only the idle thread runs
// Thread umschalten; wird aus der ISR des PITs gerufen
void preempt(); // Returns when only the idle thread runs
// Blocks current thread (move to block_queue)
void block(); // Returns on error because we don't have exceptions
// Deblock by tid (move to ready_queue)
void deblock(unsigned int tid);
};
#endif

133
src/kernel/process/Thread.asm Executable file
View File

@ -0,0 +1,133 @@
;*****************************************************************************
;* *
;* C O R O U T I N E *
;* *
;*---------------------------------------------------------------------------*
;* Beschreibung: Assemblerdarstellung der 'struct CoroutineState' aus *
;* CoroutineState.h *
;* *
;* Die Reihenfolge der Registerbezeichnungen muss unbedingt *
;* mit der von 'struct CoroutineState' uebereinstimmen. *
;* *
;* Autor: Olaf Spinczyk, TU Dortmund *
;*****************************************************************************
; EXPORTIERTE FUNKTIONEN
[GLOBAL Thread_switch]
[GLOBAL Thread_start]
; IMPLEMENTIERUNG DER FUNKTIONEN
[SECTION .text]
Thread_start:
; *
; * Hier muss Code eingefuegt werden
; *
;; NOTE: New code with pusha/popa, restores all registers as I use this not only for first start
;; == High address ==
;; ESP
;; SP --> RET ADDR
;; == Low address ==
mov esp, [esp + 0x4]
;; == High address ==
;; *OBJECT
;; 0x13115
;; *KICKOFF
;; EAX
;; ECX
;; EDX
;; EBX
;; ESP
;; EBP
;; ESI
;; EDI
;; SP --> EFLAGS
;; == Low address ==
popf
popa
;; == High address ==
;; *OBJECT
;; 0x13115
;; SP --> *KICKOFF
;; == Low address ==
sti
ret
Thread_switch:
; *
; * Hier muss Code eingefuegt werden
; *
;; NOTE: The thread switching works like this:
;; 1. Prev thread is running, pit interrupt triggers preemption, interrupt handler called
;; 2. Prev registers are pushed to prev stack after the return address
;; 3. Switch to next stack
;; 3. Registers are popped from stack, the esp now points
;; to the return address (that was written to the stack when it
;; was switched from)
;; 4. Return follows the return address to resume normal stack execution
;; == High address ==
;; ESP_NEXT
;; *ESP_PREV
;; SP --> RET ADDR
;; == Low address ==
pusha
pushf
;; == High address ==
;; + 0x2c ESP_NEXT
;; + 0x28 *ESP_PREV
;; + 0x24 RET ADDR
;; EAX
;; ECX
;; EDX
;; EBX
;; ESP
;; EBP
;; ESI
;; EDI
;; SP --> EFLAGS
;; == Low address ==
mov eax, [esp + 0x28] ; Point to *ESP_PREV (Address)
mov [eax], esp ; Update thread esp variable
;; ============================================================
mov esp, [esp + 0x2c] ; Move to next coroutines stack
;; == High address ==
;; NEW
;; THREAD
;; STACK
;; RET ADDR
;; EAX
;; ECX
;; EDX
;; EBX
;; ESP
;; EBP
;; ESI
;; EDI
;; SP --> EFLAGS
;; == Low address ==
popf ; Load new registers from stack
popa
;; == High address ==
;; NEW
;; THREAD
;; STACK
;; SP --> RET ADDR
;; == Low address ==
;; Enable interrupts again
sti
ret

133
src/kernel/process/Thread.cc Executable file
View File

@ -0,0 +1,133 @@
/*****************************************************************************
* *
* C O R O U T I N E *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines Koroutinen-Konzepts. *
* Die Koroutinen sind miteinander verkettet, weswegen die *
* Klasse Coroutine ein Subtyp von 'Chain' ist. *
* *
* Im Konstruktor wird der initialie Kontext der Koroutine *
* eingerichtet. Mit 'start' wird ein Koroutine aktiviert. *
* Das Umschalten auf die naechste Koroutine erfolgt durch *
* Aufruf von 'switchToNext'. *
* *
* Um bei einem Koroutinenwechsel den Kontext sichern zu *
* koennen, enthaelt jedes Koroutinenobjekt eine Struktur *
* CoroutineState, in dem die Werte der nicht-fluechtigen *
* Register gesichert werden koennen. *
* *
* Autor: Michael, Schoettner, HHU, 13.08.2020 *
*****************************************************************************/
#include "Thread.h"
// Funktionen, die auf der Assembler-Ebene implementiert werden, muessen als
// extern "C" deklariert werden, da sie nicht dem Name-Mangeling von C++
// entsprechen.
extern "C" {
void Thread_start(unsigned int esp);
// NOTE: Only when backing up the previous thread the esp gets updated,
// so only esp_pre is a pointer
void Thread_switch(unsigned int* esp_prev, unsigned int esp_next);
}
unsigned int ThreadCnt = 1; // Skip tid 0 as the scheduler indicates no preemption with 0
/*****************************************************************************
* Prozedur: Coroutine_init *
*---------------------------------------------------------------------------*
* Beschreibung: Bereitet den Kontext der Koroutine fuer den ersten *
* Aufruf vor. *
*****************************************************************************/
void Thread_init(unsigned int* esp, unsigned int* stack, void (*kickoff)(Thread*), void* object) {
// NOTE: c++17 doesn't allow register
// register unsigned int** sp = (unsigned int**)stack;
// unsigned int** sp = (unsigned int**)stack;
// Stack initialisieren. Es soll so aussehen, als waere soeben die
// eine Funktion aufgerufen worden, die als Parameter den Zeiger
// "object" erhalten hat.
// Da der Funktionsaufruf simuliert wird, kann fuer die Ruecksprung-
// adresse nur ein unsinniger Wert eingetragen werden. Die aufgerufene
// Funktion muss daher dafuer sorgen, dass diese Adresse nie benoetigt
// wird, sie darf also nicht terminieren, sonst kracht's.
// I thought this syntax was a bit clearer than decrementing a pointer
stack[-1] = reinterpret_cast<unsigned int>(object);
stack[-2] = 0x131155U;
stack[-3] = reinterpret_cast<unsigned int>(kickoff);
stack[-4] = 0; // EAX
stack[-5] = 0; // ECX
stack[-6] = 0; // EDX
stack[-7] = 0; // EBX
stack[-8] = reinterpret_cast<unsigned int>(&stack[-3]); // ESP
stack[-9] = 0; // EBP
stack[-10] = 0; // ESI
stack[-11] = 0; // EDI
stack[-12] = 0x200U;
*esp = reinterpret_cast<unsigned int>(&stack[-12]);
}
/*****************************************************************************
* Funktion: kickoff *
*---------------------------------------------------------------------------*
* Beschreibung: Funktion zum Starten einer Korutine. Da diese Funktion *
* nicht wirklich aufgerufen, sondern nur durch eine *
* geschickte Initialisierung des Stacks der Koroutine *
* angesprungen wird, darf er nie terminieren. Anderenfalls *
* wuerde ein sinnloser Wert als Ruecksprungadresse *
* interpretiert werden und der Rechner abstuerzen. *
*****************************************************************************/
[[noreturn]] void kickoff(Thread* object) {
object->run();
// object->run() kehrt (hoffentlich) nie hierher zurueck
while (true) {}
}
/*****************************************************************************
* Methode: Coroutine::Coroutine *
*---------------------------------------------------------------------------*
* Beschreibung: Initialer Kontext einer Koroutine einrichten. *
* *
* Parameter: *
* stack Stack für die neue Koroutine *
*****************************************************************************/
Thread::Thread(char* name) : stack(new unsigned int[1024]), esp(0), log(name), name(name), tid(ThreadCnt++) {
if (stack == nullptr) {
log.error() << "Couldn't initialize Thread (couldn't alloc stack)" << endl;
return;
}
log.info() << "Initialized thread with ID: " << tid << " (" << name << ")" << endl;
Thread_init(&esp, &stack[1024], kickoff, this); // Stack grows from top to bottom
}
/*****************************************************************************
* Methode: Coroutine::switchToNext *
*---------------------------------------------------------------------------*
* Beschreibung: Auf die nächste Koroutine umschalten. *
*****************************************************************************/
void Thread::switchTo(Thread& next) {
/* hier muss Code eingefügt werden */
// log.trace() << name << ":: Has esp " << hex << esp << endl;
Thread_switch(&esp, next.esp);
}
/*****************************************************************************
* Methode: Coroutine::start *
*---------------------------------------------------------------------------*
* Beschreibung: Aktivierung der Koroutine. *
*****************************************************************************/
void Thread::start() const {
/* hier muss Code eingefügt werden */
Thread_start(esp);
}

View File

@ -0,0 +1,69 @@
/*****************************************************************************
* *
* T H R E A D *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines kooperativen Thread-Konzepts. *
* Thread-Objekte werden vom Scheduler in einer verketteten *
* Liste 'readylist' verwaltet. *
* *
* Im Konstruktor wird der initialie Kontext des Threads *
* eingerichtet. Mit 'start' wird ein Thread aktiviert. *
* Die CPU sollte mit 'yield' freiwillig abgegeben werden. *
* Um bei einem Threadwechsel den Kontext sichern zu *
* koennen, enthaelt jedes Threadobjekt eine Struktur *
* ThreadState, in dem die Werte der nicht-fluechtigen *
* Register gesichert werden koennen. *
* *
* Zusaetzlich zum vorhandenen freiwilligen Umschalten der *
* CPU mit 'Thread_switch' gibt es nun ein forciertes Um- *
* durch den Zeitgeber-Interrupt ausgeloest wird und in *
* Assembler in startup.asm implementiert ist. Fuer das *
* Zusammenspiel mit dem Scheduler ist die Methode *
* 'prepare_preemption' in Scheduler.cc wichtig. *
* *
* Autor: Michael, Schoettner, HHU, 16.12.2016 *
*****************************************************************************/
#ifndef Thread_include__
#define Thread_include__
#include "kernel/log/Logger.h"
class Thread {
private:
unsigned int* stack;
unsigned int esp;
protected:
Thread(char* name);
NamedLogger log;
bool running = true; // For soft exit, if thread uses infinite loop inside run(), use this as condition
char* name; // For logging
unsigned int tid; // Thread-ID (wird im Konstruktor vergeben)
friend class Scheduler; // Scheduler can access tid
public:
Thread(const Thread& copy) = delete; // Verhindere Kopieren
virtual ~Thread() {
log.info() << "Uninitialized thread, ID: " << dec << tid << " (" << name << ")" << endl;
delete[] stack;
}
// Thread aktivieren
void start() const;
// Umschalten auf Thread 'next'
void switchTo(Thread& next);
// Ask thread to terminate itself
void suicide() { running = false; }
// Methode des Threads, muss in Sub-Klasse implementiert werden
virtual void run() = 0;
};
#endif

33
src/kernel/system/Globals.cc Executable file
View File

@ -0,0 +1,33 @@
/*****************************************************************************
* *
* G L O B A L S *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Globale Variablen des Systems. *
* *
* Autor: Michael Schoettner, 30.7.16 *
*****************************************************************************/
#include "Globals.h"
CGA_Stream kout; // Ausgabe-Strom fuer Kernel
const BIOS& bios = BIOS::instance(); // Schnittstelle zum 16-Bit BIOS
VESA vesa; // VESA-Treiber
PIC pic; // Interrupt-Controller
IntDispatcher intdis; // Unterbrechungsverteilung
PIT pit(10000); // 10000
PCSPK pcspk; // PC-Lautsprecher
Keyboard kb; // Tastatur
// BumpAllocator allocator;
LinkedListAllocator allocator;
// TreeAllocator allocator;
Scheduler scheduler;
KeyEventManager kevman;
SerialOut serial;
unsigned int total_mem; // RAM total
unsigned long systime = 0;

54
src/kernel/system/Globals.h Executable file
View File

@ -0,0 +1,54 @@
/*****************************************************************************
* *
* G L O B A L S *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Globale Variablen des Systems. *
* *
* Autor: Michael Schoettner, 30.7.16 *
*****************************************************************************/
#ifndef Globals_include__
#define Globals_include__
#include "device/graphics/CGA_Stream.h"
#include "device/hid/Keyboard.h"
#include "device/sound/PCSPK.h"
#include "device/time/PIT.h"
#include "device/graphics/VESA.h"
#include "kernel/memory/BumpAllocator.h"
#include "kernel/memory/LinkedListAllocator.h"
#include "kernel/memory/TreeAllocator.h"
#include "device/bios/BIOS.h"
#include "device/cpu/CPU.h"
#include "kernel/interrupt/IntDispatcher.h"
#include "device/interrupt/PIC.h"
#include "kernel/memory/Paging.h"
#include "kernel/process/Scheduler.h"
#include "device/port/SerialOut.h"
#include "kernel/event/KeyEventManager.h"
// I wanted to make more of these singletons but there were problems with atexit missing because of nostdlib I guess
extern CGA_Stream kout; // Ausgabe-Strom fuer Kernel
extern const BIOS& bios; // Schnittstelle zum 16-Bit BIOS
extern VESA vesa; // VESA-Treiber
extern PIC pic; // Interrupt-Controller
extern IntDispatcher intdis; // Unterbrechungsverteilung
extern PIT pit; // Zeitgeber
extern PCSPK pcspk; // PC-Lautsprecher
extern Keyboard kb; // Tastatur
// extern BumpAllocator allocator;
extern LinkedListAllocator allocator;
// extern TreeAllocator allocator;
extern Scheduler scheduler;
extern KeyEventManager kevman;
extern SerialOut serial;
extern unsigned int total_mem; // RAM total
extern unsigned long systime; // wird all 10ms hochgezaehlt
#endif

View File

@ -0,0 +1,41 @@
#include "Semaphore.h"
#include "kernel/system/Globals.h"
void Semaphore::p() {
// Lock to allow deterministic operations on counter/queue
lock.acquire();
if (counter > 0) {
// Semaphore can be acquired
counter = counter - 1;
lock.release();
} else {
// Block and manage thread in semaphore queue until it's woken up by v() again
if (!wait_queue.initialized()) { // TODO: I will replace this suboptimal datastructure in the future
wait_queue.reserve();
}
wait_queue.push_back(scheduler.get_active());
CPU::disable_int(); // Make sure the block() comes through after releasing the lock
lock.release();
scheduler.block(); // Moves to next thread, enables int
}
}
void Semaphore::v() {
lock.acquire();
if (!wait_queue.empty()) {
// Semaphore stays busy and unblocks next thread to work in critical section
unsigned int tid = wait_queue.front();
wait_queue.erase(wait_queue.begin());
CPU::disable_int(); // Make sure the deblock() comes through after releasing the lock
lock.release();
scheduler.deblock(tid); // Enables int
} else {
// No more threads want to work so free semaphore
counter = counter + 1;
lock.release();
}
}

39
src/lib/async/Semaphore.h Executable file
View File

@ -0,0 +1,39 @@
/*****************************************************************************
* *
* S E M A P H O R E *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung von Sempahor-Objekten. *
* *
* Autor: Michael Schoettner, 2.9.2016 *
*****************************************************************************/
#ifndef Semaphore_include__
#define Semaphore_include__
#include "kernel/process/Thread.h"
#include "SpinLock.h"
#include "lib/util/Vector.h"
class Semaphore {
private:
// Queue fuer wartende Threads.
bse::vector<unsigned int> wait_queue;
SpinLock lock;
int counter;
public:
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
// Konstruktor: Initialisieren des Semaphorzaehlers
Semaphore(int c) : wait_queue(true), counter(c) {}
// 'Passieren': Warten auf das Freiwerden eines kritischen Abschnitts.
void p();
// 'Vreigeben': Freigeben des kritischen Abschnitts.
void v();
};
#endif

64
src/lib/async/SpinLock.cc Normal file
View File

@ -0,0 +1,64 @@
/*****************************************************************************
* *
* S P I N L O C K *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines Spinlocks mithilfe der cmpxchg *
* Instruktion. *
* *
* Autor: Michael Schoettner, 2.2.2018 *
*****************************************************************************/
#include "SpinLock.h"
/*****************************************************************************
* Methode: CAS *
*---------------------------------------------------------------------------*
* Parameter: *ptr Adresse der Variable des Locks *
* old Wert gegen den verglichen wird *
* _new Wert der gesetzt werden soll *
* *
* Beschreibung: Semantik der Funktion CAS = Cmompare & Swap: *
* if old == *ptr then *
* *ptr := _new *
* return prev *
*****************************************************************************/
static inline unsigned long CAS(const unsigned long* ptr) {
unsigned long prev;
/*
AT&T/UNIX assembly syntax
The 'volatile' keyword after 'asm' indicates that the instruction
has important side-effects. GCC will not delete a volatile asm if
sit is reachable.
*/
asm volatile("lock;" // prevent race conditions with other cores
"cmpxchg %1, %2;" // %1 = _new; %2 = *ptr
// constraints
: "=a"(prev) // output: =a: RAX -> prev (%0))
: "r"(1), "m"(*ptr), "a"(0) // input = %1, %2, %3 (r=register, m=memory, a=accumlator = eax
: "memory"); // ensures assembly block will not be moved by gcc
return prev; // return pointer instead of prev to prevent unnecessary second call
}
/*****************************************************************************
* Methode: SpinLock::acquire *
*---------------------------------------------------------------------------*
* Beschreibung: Lock belegen. *
*****************************************************************************/
void SpinLock::acquire() {
// If lock == 0 the SpinLock can be aquired without waiting
// If lock == 1 the while loop blocks until aquired
while (CAS(ptr) != 0) {}
}
/*****************************************************************************
* Methode: SpinLock::release *
*---------------------------------------------------------------------------*
* Beschreibung: Lock freigeben. *
*****************************************************************************/
void SpinLock::release() {
lock = 0;
}

30
src/lib/async/SpinLock.h Normal file
View File

@ -0,0 +1,30 @@
/*****************************************************************************
* *
* S P I N L O C K *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines Spinlocks mithilfe der cmpxchg *
* Instruktion. *
* *
* Autor: Michael Schoettner, 2.2.2018 *
*****************************************************************************/
#ifndef SpinLock_include__
#define SpinLock_include__
class SpinLock {
private:
unsigned long lock;
unsigned long* ptr;
public:
SpinLock(const SpinLock& copy) = delete; // Verhindere Kopieren
SpinLock() : lock(0), ptr(&lock) {}
void acquire();
void release();
};
#endif

7
src/lib/mem/Memory.cc Executable file
View File

@ -0,0 +1,7 @@
#include "Memory.h"
void bse::memset(char* destination, const char value, std::size_t bytes) {
for (std::size_t byte = 0; byte < bytes; ++byte) {
*(destination + byte) = value;
}
}

26
src/lib/mem/Memory.h Executable file
View File

@ -0,0 +1,26 @@
#ifndef MYSTDLIB_INCLUDE_H_
#define MYSTDLIB_INCLUDE_H_
#include <utility>
namespace bse {
// add using byte or sth to replace char
template<typename T>
void memcpy(T* destination, const T* source, std::size_t count = 1) {
for (unsigned int i = 0; i < count; ++i) {
*(destination + i) = *(source + i);
}
}
void memset(char* destination, char value, std::size_t bytes);
template<typename T>
void zero(T* destination) {
memset(reinterpret_cast<char*>(destination), '\0', sizeof(T));
}
} // namespace bse
#endif

132
src/lib/mem/UniquePointer.h Normal file
View File

@ -0,0 +1,132 @@
#ifndef UniquePointer_Include_H_
#define UniquePointer_Include_H_
#include <utility>
// https://en.cppreference.com/w/cpp/memory/unique_ptr
// NOTE: Because of the way the scheduling works our functions are not executed completely.
// This means that object destructors are not called if the objects live in a scope
// that is left because of thread switching (e.g. a threads run function)...
namespace bse {
// T is the type make_unique is called with, meaning int or int[] for example
// T_ is the bare type without extents (int in both cases), so we have a
// int* pointer type for both unique_ptr<int> and unique_ptr<int[]>
template<typename T>
class unique_ptr {
private:
using T_ = std::remove_extent_t<T>;
T_* ptr = nullptr;
// Only use make_unique or reset for construction
unique_ptr(T_* ptr) : ptr(ptr) {}
// I didn't want to introduce custom deleters for my small needs
void del() {
if constexpr (std::is_array_v<T>) {
delete[] ptr;
} else {
delete ptr;
}
ptr = nullptr;
}
public:
// Forbid copying
unique_ptr(const unique_ptr& copy) = delete;
unique_ptr& operator=(const unique_ptr& copy) = delete;
// Construction
unique_ptr() = default; // Allow declaration without explicit definition
template<typename t, typename... Args>
friend typename std::enable_if_t<!std::is_array_v<t>, unique_ptr<t>>
make_unique(Args&&... args);
template<typename t>
friend typename std::enable_if_t<std::is_array_v<t>, unique_ptr<t>>
make_unique(std::size_t size);
// Deletion
~unique_ptr() {
del();
}
// Moving
unique_ptr(unique_ptr&& move) noexcept { reset(move.release()); };
// Implicit upcasting is needed: for sth like
// unique_ptr<Thread> ptr = make_unique<IdleThread>();
// IdleThread is derived from Thread so the assert passes
template<typename t>
unique_ptr(unique_ptr<t>&& move) noexcept {
static_assert(std::is_base_of_v<T, t>, "Has to be derived type");
reset(move.release());
}
unique_ptr& operator=(unique_ptr&& move) noexcept {
reset(move.release());
return *this;
}
// Resetting: Replaces managed object, deleting the old one
void reset() { del(); }
void reset(T_* pt) {
del();
ptr = pt;
}
// Release: Releases ownership without deletion
T_* release() {
// T* old = ptr;
// ptr = nullptr;
// return old;
return std::exchange(ptr, nullptr);
}
// Get: Access the raw pointer without taking ownership
T_* get() const {
return ptr;
}
// Pointer operators
T_* operator->() { return ptr; }
const T_* operator->() const { return ptr; }
T_& operator*() { return *ptr; }
const T_& operator*() const { return *ptr; }
explicit operator void*() const { return ptr; }
explicit operator bool() const { return (ptr != nullptr); }
bool operator==(const unique_ptr& other) const { return ptr == other.ptr; }
// These are only for array unique_ptr but I didn't enforce that
T_& operator[](std::size_t i) { return ptr[i]; }
const T_& operator[](std::size_t i) const { return ptr[i]; }
};
// make_unique implementation =======================================
// Allow initialization of unique_ptr<int> with optional constructor arguments
// and unique_ptr<int[]> without constructor arguments
template<typename T, typename... Args>
// We make the return type dependent on whether T is an array type or not
typename std::enable_if_t<!std::is_array_v<T>, unique_ptr<T>>
make_unique(Args&&... args) {
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T>
typename std::enable_if_t<std::is_array_v<T>, unique_ptr<T>>
make_unique(std::size_t size) {
using T_ = typename std::remove_extent_t<T>;
return unique_ptr<T>(new T_[size]);
}
} // namespace bse
#endif

84
src/lib/stream/OutStream.cc Executable file
View File

@ -0,0 +1,84 @@
/*****************************************************************************
* *
* O U T S T R E A M *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Die Klasse OutStream enthaelt die Definition des *
* << Operators fuer die wichtigsten der vordefinierten *
* Datentypen und realisiert somit die bekannte Ausgabe- *
* funktion der C++ iO_Stream Bibliothek. Zur Zeit wird *
* die Darstellung von Zeichen, Zeichenketten und ganzen *
* Zahlen unterstuetzt. Ein weiterer << Operator erlaubt *
* die Verwendung von Manipulatoren. *
* *
* Neben der Klasse OutStream sind hier auch die *
* Manipulatoren hex, dec, oct und bin fuer die Wahl der *
* Basis bei der Zahlendarstellung, sowie endl fuer den *
* Zeilenumbruch definiert. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
* Aenderungen von Michael Schoettner, HHU, 1.8.16 *
*****************************************************************************/
#include "OutStream.h"
void OutStream::put(char c) {
if (fill_width == 0) {
StringBuffer::put(c);
} else if (fill_used == fill_width - 1) {
// This indicates that content has been cut
StringBuffer::put('@');
fill_use_char();
} else if (fill_used == fill_width) {
// do nothing
} else {
StringBuffer::put(c);
fill_use_char();
}
}
void OutStream::fill_finalize() {
if (fill_width == 0 || fill_used == fill_width) {
// do nothing
} else if (fill_used > fill_width) {
// should never happen
} else {
for (unsigned char i = 0; i < fill_width - fill_used; ++i) {
StringBuffer::put(fill_char);
}
}
fill_used = 0;
}
void OutStream::fill_use_char() {
if (fill_width == 0) {
return;
}
fill_used++;
}
void OutStream::flush() {
// Flushing resets fixed width
base = 10;
fill_char = ' ';
fill_width = 0;
fill_used = 0;
}
//
// Zeichen und Zeichenketten in Stream ausgeben
//
// NOTE: The implementations now reside in the OutStream.h as I switched them to templated functions
//
// Manipulatorfunktionen
//
// Die folgenden Funktionen erhalten und liefern jeweils eine Referenz auf
// ein OutStream Objekt. Da die Klasse O_Stream einen Operator << fuer
// derartige Funktionen definiert, koennen sie mit Hilfe dieses Operators
// aufgerufen und sogar in weitere Eingaben eingebettet werden.
// Aufgabe der Manipulatoren ist, die Darstellung der nachfolgenden Ausgaben
// zu beeinflussen, z.B durch die Wahl des Zahlensystems.
// NOTE: The implementations now resides in the OutStream.h as I switched them to templated functions

235
src/lib/stream/OutStream.h Executable file
View File

@ -0,0 +1,235 @@
/*****************************************************************************
* *
* O U T S T R E A M *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Die Klasse OutStream enthaelt die Definition des *
* << Operators fuer die wichtigsten der vordefinierten *
* Datentypen und realisiert somit die bekannte Ausgabe- *
* funktion der C++ iO_Stream Bibliothek. Zur Zeit wird *
* die Darstellung von Zeichen, Zeichenketten und ganzen *
* Zahlen unterstuetzt. Ein weiterer << Operator erlaubt *
* die Verwendung von Manipulatoren. *
* *
* Neben der Klasse OutStream sind hier auch die *
* Manipulatoren hex, dec, oct und bin fuer die Wahl der *
* Basis bei der Zahlendarstellung, sowie endl fuer den *
* Zeilenumbruch definiert. *
* *
* Autor: Olaf Spinczyk, TU Dortmund *
* Aenderungen von Michael Schoettner, HHU, 06.04.20 *
*****************************************************************************/
#ifndef OutStream_include__
#define OutStream_include__
#include "lib/util/StringBuffer.h"
#include "lib/util/String.h"
#include "lib/util/StringView.h"
// Some basic width formatting
class fillw {
public:
constexpr fillw(const unsigned int w) : w(w) {}
const unsigned int w;
};
class fillc {
public:
constexpr fillc(const char c) : c(c) {}
const char c;
};
class OutStream : public StringBuffer {
private:
// Some stream formatting
unsigned char fill_used; // indicates how many characters are already used by the text internally
unsigned char fill_width; // If input is shorter than fill_width fill remaining space up with fill_char
char fill_char; // fill character for fixed width
int base; // Basis des Zahlensystems: z.B. 2, 8, 10 oder 16
void fill_use_char(); // recognizes that one char from the print width has been used up
void fill_finalize(); // does the filling after text has been written to buffer
public:
OutStream(const OutStream& copy) = delete; // Verhindere Kopieren
OutStream() : fill_used(0), fill_width(0), fill_char(' '), base(10) {}
// ~OutStream() override = default;
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
void flush() override;
void put(char c) override;
// OPERATOR << : Umwandlung des angegebenen Datentypes in eine
// Zeichenkette.
// NOTE: I changed the stream manipulators to templates to be usable with different streams.
// If a Stream derived from OutStream gets passed to a operator<< the type won't be "lowered".
// This allows chaining of operator<< of different streams.
// Needed because I added operator<< overloads to the CGA_Stream class to change color with manipulators.
// Darstellung eines Zeichens (trivial)
template<typename T>
friend T& operator<<(T& os, char c) {
os.put(c);
if (c != '\n') {
// endl() doesn't has access to StringBuffer::put(), so ignore \n here
os.fill_finalize();
}
return os;
}
template<typename T>
friend T& operator<<(T& os, unsigned char c) { return os << static_cast<char>(c); }
// Darstellung einer nullterminierten Zeichenkette
template<typename T>
friend T& operator<<(T& os, const bse::string_view string) {
for (char current : string) {
os.put(current);
}
os.fill_finalize();
return os;
}
// Darstellung ganzer Zahlen im Zahlensystem zur Basis base
template<typename T>
friend T& operator<<(T& os, short ival) { return os << static_cast<long>(ival); }
template<typename T>
friend T& operator<<(T& os, unsigned short ival) { return os << static_cast<unsigned long>(ival); }
template<typename T>
friend T& operator<<(T& os, int ival) { return os << static_cast<long>(ival); }
template<typename T>
friend T& operator<<(T& os, unsigned int ival) { return os << static_cast<unsigned long>(ival); }
template<typename T>
friend T& operator<<(T& os, long ival) {
// Bei negativen Werten wird ein Minuszeichen ausgegeben.
if (ival < 0) {
os.put('-');
ival = -ival;
}
// Dann wird der Absolutwert als vorzeichenlose Zahl ausgegeben.
return os << static_cast<unsigned long>(ival);
}
template<typename T>
friend T& operator<<(T& os, unsigned long ival) {
unsigned long div;
char digit;
if (os.base == 8) {
os.put('0'); // oktale Zahlen erhalten eine fuehrende Null
} else if (os.base == 16) {
os.put('0'); // hexadezimale Zahlen ein "0x"
os.put('x');
}
// Bestimmung der groessten Potenz der gewaehlten Zahlenbasis, die
// noch kleiner als die darzustellende Zahl ist.
for (div = 1; ival / div >= static_cast<unsigned long>(os.base); div *= os.base) {}
// ziffernweise Ausgabe der Zahl
for (; div > 0; div /= static_cast<unsigned long>(os.base)) {
digit = ival / div;
if (digit < 10) {
os.put('0' + digit);
} else {
os.put('a' + digit - 10);
}
ival %= div;
}
os.fill_finalize();
return os;
}
// Darstellung eines Zeigers als hexadezimale ganze Zahl
template<typename T>
friend T& operator<<(T& os, void* ptr) {
int oldbase = os.base;
os.base = 16;
os << reinterpret_cast<unsigned long>(ptr);
os.base = oldbase;
return os;
}
// Aufruf einer Manipulatorfunktion
template<typename T>
friend T& operator<<(T& os, T& (*f)(T&)) { return f(os); }
// For stream formatting
template<typename T>
friend T& operator<<(T& os, const fillw& w) {
os.flush(); // Flush the buffer to not modify previous output
os.fill_width = w.w;
return os;
}
template<typename T>
friend T& operator<<(T& os, const fillc& c) {
os.flush();
os.fill_char = c.c;
return os;
}
// Allow access to base member
template<typename T> friend T& endl(T& os);
template<typename T> friend T& bin(T& os);
template<typename T> friend T& oct(T& os);
template<typename T> friend T& dec(T& os);
template<typename T> friend T& hex(T& os);
};
//
// Manipulatorfunktionen
//
// Die folgenden Funktionen erhalten und liefern jeweils eine Referenz auf
// ein OutStream Objekt. Da die Klasse OutStream einen Operator << fuer
// derartige Funktionen definiert, koennen sie mit Hilfe dieses Operators
// aufgerufen und sogar in weitere Eingaben eingebettet werden.
// Aufgabe der Manipulatoren ist, die Darstellung der nachfolgenden Ausgaben
// zu beeinflussen, z.B durch die Wahl des Zahlensystems.
// Zeilenumbruch in Ausgabe einfuegen.
template<typename T>
T& endl(T& os) {
// os << '\r';
os << '\n';
os.flush();
return os;
}
// Waehle binaeres Zahlensystem aus.
template<typename T>
T& bin(T& os) {
os.base = 2;
return os;
}
// Waehle oktales Zahlensystem aus.
template<typename T>
T& oct(T& os) {
os.base = 8;
return os;
}
// Waehle dezimales Zahlensystem aus.
template<typename T>
T& dec(T& os) {
os.base = 10;
return os;
}
// Waehle hexadezimales Zahlensystem aus.
template<typename T>
T& hex(T& os) {
os.base = 16;
return os;
}
#endif

62
src/lib/util/Array.h Normal file
View File

@ -0,0 +1,62 @@
#ifndef ARRAY_INCLUDE_H
#define ARRAY_INCLUDE_H
#include "Iterator.h"
#include <utility>
namespace bse {
template<typename T, const std::size_t N>
class array {
public:
using iterator = ContinuousIterator<T>;
private:
T buf[N];
public:
array() = default; // If i write default something like bse::array<int, 10> arr; is not initialized...
// Construct like this: bse::array<int, 5> {1, 2, 3, 4, 5};
array(std::initializer_list<T> list) {
typename std::initializer_list<T>::iterator it = list.begin();
for (unsigned int i = 0; i < N; ++i) {
buf[i] = *it;
++it;
}
}
iterator begin() { return iterator(&buf[0]); }
iterator begin() const { return iterator(&buf[0]); }
iterator end() { return iterator(&buf[N]); }
iterator end() const { return iterator(&buf[N]); }
constexpr T& operator[](std::size_t i) { return buf[i]; }
constexpr const T& operator[](std::size_t i) const { return buf[i]; }
T* data() { return &buf[0]; }
const T* data() const { return &buf[0]; }
void swap(array<T, N>& other) {
for (std::size_t i = 0; i < N; ++i) {
std::swap(buf[i], other[i]);
}
}
// Array& other has to have size n:
// arr1.swap_n<5>(arr2) => arr2 has size 5, arr1 has size >= 5
template<std::size_t n>
void swap_n(array<T, n>& other) {
for (std::size_t i = 0; i < n; ++i) {
std::swap(buf[i], other[i]);
}
}
constexpr std::size_t size() const {
return N;
}
};
} // namespace bse
#endif

60
src/lib/util/Iterator.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef Iterator_Include_H_
#define Iterator_Include_H_
namespace bse {
// This iterator works for structures where the elements are adjacent in memory.
template<typename T>
class ContinuousIterator {
private:
T* ptr = nullptr;
public:
ContinuousIterator() = delete;
// Use const_cast as the iterator has to increment the pointer
ContinuousIterator(const T* ptr) : ptr(const_cast<T*>(ptr)) {}
ContinuousIterator& operator++() {
++ptr;
return *this;
}
ContinuousIterator& operator--() {
--ptr;
return *this;
}
ContinuousIterator operator+(unsigned int add) {
return ContinuousIterator(ptr + add);
}
ContinuousIterator operator-(unsigned int sub) {
return ContinuousIterator(ptr - sub);
}
// Convenience
T* operator->() { return ptr; }
const T* operator->() const { return ptr; }
T& operator*() { return *ptr; }
const T& operator*() const { return *ptr; }
bool operator<(const ContinuousIterator& other) const { return ptr < other.ptr; }
bool operator<=(const ContinuousIterator& other) const { return ptr <= other.ptr; }
bool operator>(const ContinuousIterator& other) const { return ptr > other.ptr; }
bool operator>=(const ContinuousIterator& other) const { return ptr >= other.ptr; }
bool operator==(const ContinuousIterator& other) const { return ptr == other.ptr; }
bool operator!=(const ContinuousIterator& other) const { return ptr != other.ptr; }
template<typename t>
friend unsigned int distance(const ContinuousIterator<t>& first, const ContinuousIterator<t>& last);
};
template<typename T>
unsigned int distance(const ContinuousIterator<T>& first, const ContinuousIterator<T>& last) {
return last.ptr - first.ptr;
}
} // namespace bse
#endif

Some files were not shown because too many files have changed in this diff Show More