Skip to content

Commit

Permalink
Initial kernel source file implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed May 15, 2024
1 parent badc1ca commit fb0c9cb
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 0 deletions.
105 changes: 105 additions & 0 deletions kernel/jessy_os.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* This file is part of the Jessy OS distribution (https://github.com/nthnn/Jessy-OS).
* Copyright (c) 2024 Nathanne Isip.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "jessy_os.h"
#include "jessy_os_configs.h"

#include <fabgl.h>
#include <SD.h>

fabgl::ILI9341Controller DisplayController;
fabgl::PS2Controller PS2Controller;
fabgl::Terminal Terminal;

SPIClass sdSpi(HSPI);

void JessyOS::initDevices() {
PS2Controller.begin(PS2Preset::KeyboardPort0);

DisplayController.begin(TFT_SCK, TFT_MOSI, TFT_DC, TFT_RESET, TFT_CS, TFT_SPIBUS);
DisplayController.setResolution("\"TFT_320x240\" 320 240");
}

void JessyOS::initTerminal() {
Terminal.begin(&DisplayController);
Terminal.loadFont(&fabgl::FONT_8x14);
Terminal.setBackgroundColor(Color::Black);
Terminal.setForegroundColor(Color::White);
Terminal.connectLocally();
Terminal.clear();
Terminal.enableCursor(true);
}

void JessyOS::initSDCard() {
sdSpi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);

if(!SD.begin(SD_CS, sdSpi, 80000000)) {
Terminal.println("Card \e[94mMount\e[97m Failed");

do sdSpi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
while(!SD.begin(SD_CS, sdSpi, 80000000));

Terminal.clear();
}
}

void JessyOS::initPSRAM() {
if(!psramInit()) {
Terminal.println("\e[94mCannot\e[97m initialize PSRAM.");
while(true);
}
}

void JessyOS::startVM(RishkaVM* vm) {
vm = new RishkaVM();
vm->initialize(&Terminal);

Terminal.onVirtualKeyItem = [&](VirtualKeyItem * vkItem) {
if(vkItem->CTRL && vkItem->vk == VirtualKey::VK_c && vm->isRunning()) {
vm->stopVM();
vm->reset();

Terminal.println("^C");
}
};
}

void JessyOS::loadBoot(RishkaVM* vm) {
if(!vm->loadFile("/bin/boot.bin")) {
Terminal.println("Failed to \e[94mload\e[97m boot binary.");
while(true);
}

vm->run(0, NULL);
if(vm->getExitCode() != 0)
while(true);
}

void JessyOS::print(String text) {
Terminal.print(text);
}

void JessyOS::println(String text) {
Terminal.println(text);
}

String JessyOS::readLine() {
fabgl::LineEditor line(&Terminal);
line.edit();

return String(line.get());
}
38 changes: 38 additions & 0 deletions kernel/jessy_os.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of the Jessy OS distribution (https://github.com/nthnn/Jessy-OS).
* Copyright (c) 2024 Nathanne Isip.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef JESSY_OS_H
#define JESSY_OS_H

#include <rishka.h>

class JessyOS final {
public:
static void initDevices();
static void initTerminal();
static void initSDCard();
static void initPSRAM();

static void startVM(RishkaVM* vm);
static void loadBoot(RishkaVM* vm);

static void print(String text);
static void println(String text);
static String readLine();
};

#endif
35 changes: 35 additions & 0 deletions kernel/jessy_os_configs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of the Jessy OS distribution (https://github.com/nthnn/Jessy-OS).
* Copyright (c) 2024 Nathanne Isip.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef JESSY_OS_CONFIGS_H
#define JESSY_OS_CONFIGS_H

#include <SPI.h>

#define TFT_CS 5 // TFT SPI select pin
#define TFT_SCK 18 // TFT SPI clock pin
#define TFT_MOSI 23 // TFT SPI MOSI pin
#define TFT_DC 15 // TFT data/command pin
#define TFT_RESET 4 // TFT reset pin
#define TFT_SPIBUS VSPI_HOST // TFT SPI bus

#define SD_CS 2 // SD card chip select pin
#define SD_SCK 14 // SD card SPI clock pin
#define SD_MOSI 13 // SD card SPI MOSI pin
#define SD_MISO 12 // SD card SPI MISO pin

#endif
109 changes: 109 additions & 0 deletions kernel/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* This file is part of the Jessy OS distribution (https://github.com/nthnn/Jessy-OS).
* Copyright (c) 2024 Nathanne Isip.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "jessy_os.h"
#include <rishka.h>

RishkaVM* vm;

void splitString(const String& input, char** tokens, int maxTokens, int &count) {
int tokenCount = 0, tokenStart = 0;
bool inQuotes = false;

for(int i = 0; i < input.length(); i++) {
if(input[i] == '"')
inQuotes = !inQuotes;
else if(input[i] == ' ' && !inQuotes) {
if(tokenCount < maxTokens) {
input.substring(tokenStart, i)
.toCharArray(tokens[tokenCount], i - tokenStart + 1);

tokens[tokenCount++][i - tokenStart] = '\0';
tokenStart = i + 1;
count++;
}
else break;
}
}

if(tokenCount < maxTokens && tokenStart < input.length()) {
input.substring(tokenStart).toCharArray(
tokens[tokenCount],
input.length() - tokenStart + 1
);

tokens[tokenCount++][input.length() - tokenStart] = '\0';
count++;
}
}

void setup() {
JessyOS::initDevices();
JessyOS::initTerminal();
JessyOS::initSDCard();
JessyOS::initPSRAM();

JessyOS::startVM(vm);
JessyOS::loadBoot(vm);

JessyOS::print("\e[32m[\e[97m" +
vm->getWorkingDirectory() +
"\e[97m\e[32m]~\e[97m "
);
}

void loop() {
String input = JessyOS::readLine();
if(input == "") {
JessyOS::print(String("\e[32m[\e[97m") +
vm->getWorkingDirectory() +
"\e[97m\e[32m]~\e[97m "
);

return;
}

char* tokens[10];
int count = 0;

for(int i = 0; i < 10; i++)
tokens[i] = (char*) malloc(50 * sizeof(char));
splitString(input, tokens, 10, count);

if(!vm->loadFile(tokens[0])) {
JessyOS::println("Failed to \e[94mload\e[97m specified file: " +
String(tokens[0]));

for(int i = 0; i < 10; i++)
free(tokens[i]);

vm->reset();

JessyOS::print(String("\e[32m[\e[97m") +
vm->getWorkingDirectory() + "\e[97m\e[32m]~\e[97m ");
return;
}

vm->run(count, tokens);
vm->reset();

for(int i = 0; i < 10; i++)
free(tokens[i]);

JessyOS::print(String("\e[32m[\e[97m") +
vm->getWorkingDirectory() + "\e[97m\e[32m]~\e[97m ");
}

0 comments on commit fb0c9cb

Please sign in to comment.