From c8f500470ec2149c116e1d3025abf17d95c87774 Mon Sep 17 00:00:00 2001
From: able <abl3theabove@gmail.com>
Date: Sat, 28 Oct 2023 08:28:07 -0500
Subject: [PATCH] Getting some basic userland logger setup

---
 Cargo.lock                     |  6 +++---
 kernel/src/holeybytes/ecah.rs  | 17 ++++++-----------
 kernel/src/kmain.rs            |  3 ++-
 repbuild/hblib/readme.md       |  1 +
 repbuild/hblib/std.rhai        | 25 +++++++++++++++++++++++++
 repbuild/holeybytes/ecall.rhai |  2 +-
 repbuild/holeybytes/main.rhai  | 13 +++++++++++++
 repbuild/limine.cfg            |  3 +++
 repbuild/src/main.rs           |  5 +++++
 9 files changed, 59 insertions(+), 16 deletions(-)
 create mode 100644 repbuild/hblib/readme.md
 create mode 100644 repbuild/hblib/std.rhai
 create mode 100644 repbuild/holeybytes/main.rhai

diff --git a/Cargo.lock b/Cargo.lock
index bd33941..6bf0825 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -457,7 +457,7 @@ dependencies = [
 [[package]]
 name = "hbasm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#fbc68fadc3fc84018aa57322ef93af8fe7ec7d10"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#0f619887c613be3fb3a7dd8c970b7ee76ad0d23b"
 dependencies = [
  "paste",
  "rhai",
@@ -467,7 +467,7 @@ dependencies = [
 [[package]]
 name = "hbbytecode"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/ableos/holey-bytes#7b1f6d535db3ac4163b45967d963d31f2c577898"
+source = "git+https://git.ablecorp.us/ableos/holey-bytes#0f619887c613be3fb3a7dd8c970b7ee76ad0d23b"
 dependencies = [
  "with_builtin_macros",
 ]
@@ -475,7 +475,7 @@ dependencies = [
 [[package]]
 name = "hbvm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/ableos/holey-bytes#7b1f6d535db3ac4163b45967d963d31f2c577898"
+source = "git+https://git.ablecorp.us/ableos/holey-bytes#0f619887c613be3fb3a7dd8c970b7ee76ad0d23b"
 dependencies = [
  "hbbytecode",
 ]
diff --git a/kernel/src/holeybytes/ecah.rs b/kernel/src/holeybytes/ecah.rs
index 8cce65d..7deaf01 100644
--- a/kernel/src/holeybytes/ecah.rs
+++ b/kernel/src/holeybytes/ecah.rs
@@ -7,14 +7,12 @@ use {
 };
 
 pub fn handler(vm: &mut Vm) {
-    let r255 = vm.registers[255].cast::<u64>();
-    let r254 = vm.registers[254].cast::<u64>();
-    let r253 = vm.registers[253].cast::<u64>();
+    let r1 = vm.registers[1].cast::<u64>();
 
-    debug!("Ecall number {:?}", r255);
+    debug!("Ecall number {:?}", r1);
     trace!("Register dump: {:?}", vm.registers);
 
-    match r255 {
+    match r1 {
         0 => {
             // TODO: explode computer
             // hello world ecall
@@ -24,11 +22,8 @@ pub fn handler(vm: &mut Vm) {
         }
         1 => {
             // Make buffer
-            let r255 = vm.registers[255].cast::<u64>();
-            let r254 = vm.registers[254].cast::<u64>();
-            let r253 = vm.registers[253].cast::<u64>();
 
-            let bounded = match r254 {
+            let bounded = match vm.registers[2].cast::<u64>() {
                 0 => false,
                 1 => true,
                 _ => {
@@ -36,7 +31,7 @@ pub fn handler(vm: &mut Vm) {
                 }
             };
 
-            let length = r254;
+            let length = vm.registers[3].cast::<u64>();
 
             let mut buffs = IPC_BUFFERS.lock();
             let abc;
@@ -80,7 +75,7 @@ pub fn handler(vm: &mut Vm) {
         // 4
         // 5
         _ => {
-            log::error!("Syscall unknown {:?}", r255)
+            log::error!("Syscall unknown {:?}", r1);
         }
     }
 }
diff --git a/kernel/src/kmain.rs b/kernel/src/kmain.rs
index 65c05a1..36041dd 100644
--- a/kernel/src/kmain.rs
+++ b/kernel/src/kmain.rs
@@ -44,8 +44,9 @@ pub fn kmain(cmdline: &str, boot_modules: BootModules) -> ! {
     // TODO: schedule the init system from the initramfs
 
     let mut executor = crate::task::Executor::default();
+    let bm_take = boot_modules.len();
     unsafe {
-        for module in boot_modules.into_iter().take(2) {
+        for module in boot_modules.into_iter().take(bm_take) {
             executor.spawn(async move {
                 if let Err(e) = ExecThread::new(&module.bytes, Address::new(0)).await {
                     log::error!("{e:?}");
diff --git a/repbuild/hblib/readme.md b/repbuild/hblib/readme.md
new file mode 100644
index 0000000..768cf3c
--- /dev/null
+++ b/repbuild/hblib/readme.md
@@ -0,0 +1 @@
+A kind of standard library.
\ No newline at end of file
diff --git a/repbuild/hblib/std.rhai b/repbuild/hblib/std.rhai
new file mode 100644
index 0000000..82814cf
--- /dev/null
+++ b/repbuild/hblib/std.rhai
@@ -0,0 +1,25 @@
+
+fn ipc_send(buffer_id, mem_addr, length){
+    // set the ecall
+    li8(r1, 3);
+    // Set the buffer ID to be the BufferID
+    li64(r2, buffer_id);
+    lra(r3, r0, mem_addr);
+    // set the length
+    li64(r4, length);
+    // ecall
+    eca(); 
+}
+
+private fn log(log_level, string){
+    let str = data::str(string);
+    ipc_send(1, str, str.len);
+}
+
+fn Error(string) {log(0, string);}
+fn Warn(string) {log(1, string);}
+fn Info(string) {log(2, string);}
+// Due to rhai limitations this cannot be debug
+// because of this all of the log levels are upper case
+fn Debug(string) {log(3, string);}
+fn Trace(string) {log(4, string);}
diff --git a/repbuild/holeybytes/ecall.rhai b/repbuild/holeybytes/ecall.rhai
index 556d6f1..996e706 100644
--- a/repbuild/holeybytes/ecall.rhai
+++ b/repbuild/holeybytes/ecall.rhai
@@ -1,3 +1,3 @@
 li8 (r1, 0x69);
-eca ();
+// eca ();
 tx  ();
diff --git a/repbuild/holeybytes/main.rhai b/repbuild/holeybytes/main.rhai
new file mode 100644
index 0000000..803e937
--- /dev/null
+++ b/repbuild/holeybytes/main.rhai
@@ -0,0 +1,13 @@
+import "repbuild/hblib/std" as std;
+
+fn main(){
+    std::Error(":+)");
+    std::Warn("Your mom fell in a well!");
+    std::Info("Hello, world!");
+    std::Debug("ABC");
+    std::Trace("Trace Deez");
+
+    tx();
+}
+
+main();
\ No newline at end of file
diff --git a/repbuild/limine.cfg b/repbuild/limine.cfg
index 4580ced..840548e 100644
--- a/repbuild/limine.cfg
+++ b/repbuild/limine.cfg
@@ -23,3 +23,6 @@ TERM_BACKDROP=008080
 
     MODULE_PATH=boot:///ecall.hbf
     MODULE_CMDLINE=""
+
+    MODULE_PATH=boot:///main.hbf
+    MODULE_CMDLINE=""
diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs
index f522974..41a010c 100644
--- a/repbuild/src/main.rs
+++ b/repbuild/src/main.rs
@@ -157,6 +157,11 @@ fn get_fs() -> Result<FileSystem<impl ReadWriteSeek>, io::Error> {
         &mut fs.root_dir().create_file("ecall.hbf")?,
     )?;
 
+    io::copy(
+        &mut File::open("target/holeybytes/main.hbf")?,
+        &mut fs.root_dir().create_file("main.hbf")?,
+    )?;
+
     drop(bootdir);
     Ok(fs)
 }