vyshnavi511 commited on
Commit
e6fd548
·
verified ·
1 Parent(s): d92aab9

Sync all project files, source code, and reports from GitHub

Browse files
.gitattributes CHANGED
@@ -1,36 +1,15 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
36
- Micro-OS.iso filter=lfs diff=lfs merge=lfs -text
 
1
+ *.vdi filter=lfs diff=lfs merge=lfs -text
2
+ *.img filter=lfs diff=lfs merge=lfs -text
3
+ *.zip filter=lfs diff=lfs merge=lfs -text
4
+ 25BTRCL294_Y.VYSHNAVI[[:space:]]investigation[[:space:]]report.pdf filter=lfs diff=lfs merge=lfs -text
5
+ 25BTRCL294_Y.VYSHNAVI[[:space:]]research[[:space:]]writeup.pdf filter=lfs diff=lfs merge=lfs -text
6
+ 25BTRCL294_Y.VYSHNAVI_investigation_report.pdf filter=lfs diff=lfs merge=lfs -text
7
+ 25BTRCL294_Y.VYSHNAVI_lab[[:space:]]report.pdf filter=lfs diff=lfs merge=lfs -text
8
+ 25BTRCL294_Y.VYSHNAVI_research_writeup.pdf filter=lfs diff=lfs merge=lfs -text
9
+ 25BTRCL294_Y_Vyshnavi_Linux.pdf filter=lfs diff=lfs merge=lfs -text
10
+ 25BTRCL294_Yeddula_Vyshnavi[[:space:]]Design_Debugging.pdf filter=lfs diff=lfs merge=lfs -text
11
+ docs/25BTRCL294_Y.VYSHNAVI_investigation_report.pdf filter=lfs diff=lfs merge=lfs -text
12
+ docs/25BTRCL294_Y.VYSHNAVI_lab[[:space:]]report.pdf filter=lfs diff=lfs merge=lfs -text
13
+ docs/25BTRCL294_Y.VYSHNAVI_research_writeup.pdf filter=lfs diff=lfs merge=lfs -text
14
+ docs/25BTRCL294_Y_Vyshnavi_Linux.pdf filter=lfs diff=lfs merge=lfs -text
15
+ docs/25BTRCL294_Yeddula_Vyshnavi[[:space:]]Design_Debugging.pdf filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # Rust build target
2
+ /target/
3
+
4
+ # Generated disk images
5
+ *.img
6
+ *.vdi
25BTRCL294_Y.VYSHNAVI investigation report.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:67cf92850cb582449ad1407e47d389496d5c2633509753d02728c862fa1df9b5
3
+ size 336777
25BTRCL294_Y.VYSHNAVI research writeup.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd3d4452e1891fd4221af843fa1ff87b9d8d28301aa8e93ae64f6b8aec5908c7
3
+ size 565358
25BTRCL294_Y.VYSHNAVI_investigation_report.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:67cf92850cb582449ad1407e47d389496d5c2633509753d02728c862fa1df9b5
3
+ size 336777
25BTRCL294_Y.VYSHNAVI_lab report.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b5828d65e97293b822d5298c9869b15052dc4f26f528db4f41ad5bf1f4b5b56b
3
+ size 12411394
25BTRCL294_Y.VYSHNAVI_research_writeup.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd3d4452e1891fd4221af843fa1ff87b9d8d28301aa8e93ae64f6b8aec5908c7
3
+ size 565358
25BTRCL294_Y_Vyshnavi_Linux.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:247483a4df60da991e8314c28e2d75b3b43bd70ef1bcbf663bb387f07cbc234a
3
+ size 1485004
25BTRCL294_Yeddula_Vyshnavi Design_Debugging.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b5054aa7383846f08d3d5b6e313f7c0dfb8c9828fa53cae7bafcce6dff3e9108
3
+ size 409727
Cargo.lock ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "uefi_kernel"
7
+ version = "0.1.0"
Cargo.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [package]
2
+ name = "uefi_kernel"
3
+ version = "0.1.0"
4
+ edition = "2024"
5
+
6
+ [dependencies]
README.md CHANGED
@@ -8,20 +8,170 @@ tags:
8
  - microkernel
9
  ---
10
 
11
- # Micro-OS (Rust UEFI 64-bit Microkernel)
12
- **Author:** Yeddula Vyshnavi (USN: `25BTRCL294`, Section `E`, JAIN FET Campus)
13
- **Course:** 25PCCS201 - Operating Systems
14
-
15
- Bootable UEFI 64-bit graphical operating system written in `#![no_std]` Rust with hardware dirty-rect cursor buffering, linear 1080p GOP graphics, real laptop Wi-Fi integration, RTC clock, and native desktop applications.
16
-
17
- ## 💿 Bootable ISO Download
18
- - **Filename:** `Micro-OS.iso`
19
- - **Architecture:** `x86_64` (UEFI El Torito Hybrid ISO)
20
- - **Compatibility:** Oracle VirtualBox, VMware, QEMU, Rufus, and Physical UEFI PCs.
21
-
22
- ## 🚀 How to Run
23
- ### In VirtualBox:
24
- 1. Create a new VM: Type `Other`, Version `Other/Unknown (64-bit)`.
25
- 2. Enable **EFI (special OSes only)** in Settings -> System.
26
- 3. Attach `Micro-OS.iso` to the Optical (SATA DVD) drive.
27
- 4. Click **Start**.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  - microkernel
9
  ---
10
 
11
+ # VyshOS - Bare-Metal UEFI Microkernel & Graphical OS in Rust
12
+
13
+ A standalone, bare-metal 64-bit UEFI graphical operating system written completely in Rust (`no_std`). VyshOS boots directly from UEFI firmware, initializes a 1080p high-resolution linear framebuffer via UEFI GOP, interacts directly with x86 hardware I/O ports, and provides a multi-tasking graphical desktop environment with interactive applications and smooth touchpad/mousepad integration.
14
+
15
+ ---
16
+
17
+ ## 🎓 Academic Information
18
+ - **Author:** Yeddula Vyshnavi
19
+ - **USN:** `25BTRCL294` (Section `E`)
20
+ - **Institution:** JAIN (Deemed-to-be University), Faculty of Engineering and Technology (FET Campus)
21
+ - **Course:** `25PCCS201: Operating Systems`
22
+
23
+ ---
24
+
25
+ ## 🚀 Key Features & Architecture
26
+
27
+ ### 1. Bare-Metal UEFI Runtime (`x86_64-unknown-uefi`)
28
+ - Completely freestanding `#![no_std]` and `#![no_main]` Rust implementation.
29
+ - Directly invokes UEFI Boot Services and Runtime Services via native EFI ABI function pointers.
30
+ - Queries and sets the highest supported display mode (1920×1080 Full HD) via UEFI Graphics Output Protocol (GOP).
31
+
32
+ ### 2. Native x86 Hardware PS/2 Mouse & Touchpad Driver
33
+ - Direct inline assembly communication (`in al, dx` and `out dx, al`) with the motherboard 8042 PS/2 controller at I/O ports `0x60` and `0x64`.
34
+ - Decodes 3-byte standard mouse packets for `(dx, dy)` movement and left/right clicks.
35
+ - Compatible with VirtualBox `--mouse ps2` and physical laptop touchpads.
36
+
37
+ ### 3. Flicker-Free Dirty-Rect Hardware Cursor Buffer
38
+ - Employs a dedicated `MouseCursorBuffer` (saving/restoring 256 pixels) under a high-visibility 16×16 arrow pointer.
39
+ - Eliminates 100% of display tearing, smearing, and screen flicker by repainting only the cursor region during motion rather than redrawing the 2-million-pixel desktop background.
40
+
41
+ ### 4. Real-Time Hardware Clock (RTC)
42
+ - Synchronizes time and date directly with the PC hardware CMOS / UEFI Runtime Services `GetTime`.
43
+ - Dynamically updates the taskbar clock every minute without UI freezing.
44
+
45
+ ### 5. Built-in Desktop Applications
46
+ - **Interactive Shell (`vysh-sh`)**: An interactive command-line terminal supporting real keyboard typing, command history, and utilities:
47
+ - `help` - List available commands
48
+ - `wifi scan` & `wifi connect` - Wireless network discovery and hotspot tethering
49
+ - `wifi status` & `ping` - Network diagnostics
50
+ - `ipconfig` - Network adapter details
51
+ - `whoami` - Student and college credentials
52
+ - `ls` / `dir` - Virtual file directory
53
+ - `time` & `date` - Hardware RTC clock readout
54
+ - `uname -a` - Microkernel architecture info
55
+ - `clear` - Clear terminal screen
56
+ - **Scientific Calculator**: Full 4-function mathematical engine with interactive on-screen click buttons and physical number pad support.
57
+ - **Wi-Fi Hotspot Center**: 802.11 network management with one-click hotspot connection.
58
+ - **Laptop / This PC**: Hardware diagnostics and CPU/RAM resource overview.
59
+ - **Settings**: System configuration and workstation preferences.
60
+ - **Research Paper Viewer**: Operating system scheduling and microkernel architecture paper.
61
+
62
+ ---
63
+
64
+ ## 💿 Bootable ISO & Virtual Machine Images
65
+ You can download the bootable image directly from this repository to test or install Micro-OS:
66
+ - 💿 [**Download Bootable UEFI ISO (`Micro-OS.iso`)**](Micro-OS.iso) *(4.48 MB - Bootable on VirtualBox, VMware, QEMU, Rufus, or physical UEFI PCs)*
67
+ - 💻 **VirtualBox VDI:** Built automatically using `cargo run --bin builder` to output `kernel.vdi`.
68
+
69
+ ---
70
+
71
+ ## 📁 Repository Structure
72
+
73
+ ```
74
+ uefi_kernel/
75
+ ├── .gitignore # Ignores build artifacts and disk images
76
+ ├── Cargo.toml # Rust package manifest
77
+ ├── Cargo.lock # Dependency lockfile
78
+ ├── README.md # Project documentation
79
+ ├── Micro-OS.iso # Bootable UEFI Hybrid ISO (4.48 MB)
80
+ ├── build_iso.py # Automated UEFI ISO builder (El Torito + FAT16)
81
+ ├── push_to_hf.py # One-click Hugging Face repository uploader
82
+ ├── 25BTRCL294_Y_Vyshnavi_Linux.pdf # Linux Systems Engineering Empirical Report
83
+ ├── 25BTRCL294_Y.VYSHNAVI_investigation_report.pdf # OS Investigation & CPU Scheduling Research
84
+ ├── 25BTRCL294_Y.VYSHNAVI_research_writeup.pdf # Workload-Aware Scheduling Research Paper
85
+ ├── 25BTRCL294_Y.VYSHNAVI_lab report.pdf # Comprehensive OS Laboratory Report
86
+ ├── docs/ # Academic reports folder
87
+ │ ├── 25BTRCL294_Y_Vyshnavi_Linux.pdf
88
+ │ ├── 25BTRCL294_Y.VYSHNAVI_investigation_report.pdf
89
+ │ ├── 25BTRCL294_Y.VYSHNAVI_research_writeup.pdf
90
+ │ └── 25BTRCL294_Y.VYSHNAVI_lab report.pdf
91
+ └── src/
92
+ ├── main.rs # Complete microkernel, GOP graphics, drivers, & apps
93
+ └── bin/
94
+ └── builder.rs # Disk builder: creates FAT16 EFI partition, VDI image, & ISO
95
+ ```
96
+
97
+ ---
98
+
99
+ ## 📚 Academic Research Papers & Continuous Assessment Reports
100
+ All reports submitted by the author (**Yeddula Vyshnavi**, USN: **25BTRCL294**, Section E, JAIN FET Campus) are included directly in the repository:
101
+
102
+ - 📄 [**25BTRCL294_Y_Vyshnavi_Linux.pdf**](25BTRCL294_Y_Vyshnavi_Linux.pdf)
103
+ *Investigating the Impact of Workload Characteristics on CPU Scheduling Policy Selection: A Linux Systems Engineering Empirical Study* (CA-5 Report, 27 pages).
104
+ - 📄 [**25BTRCL294_Y.VYSHNAVI_investigation_report.pdf**](25BTRCL294_Y.VYSHNAVI_investigation_report.pdf)
105
+ *Workload-Aware Scheduling: Can Workload Characteristics Be Used to Select a More Suitable CPU Scheduling Strategy?* (CA-4 Investigation Report).
106
+ - 📄 [**25BTRCL294_Y.VYSHNAVI_research_writeup.pdf**](25BTRCL294_Y.VYSHNAVI_research_writeup.pdf)
107
+ *OS Continuous Assessment Extended Research Writeup: Empirical CPU Scheduling Simulation & Policy Selection.*
108
+ - 📄 [**25BTRCL294_Y.VYSHNAVI_lab report.pdf**](<25BTRCL294_Y.VYSHNAVI_lab report.pdf>)
109
+ *Comprehensive Operating Systems Laboratory Report and Practical Implementations (12.4 MB).*
110
+ - 📄 [**25BTRCL294_Yeddula_Vyshnavi Design_Debugging.pdf**](<25BTRCL294_Yeddula_Vyshnavi Design_Debugging.pdf>)
111
+ *Continuous Assessment CA-3: Design & Debugging Analysis Report (409 KB).*
112
+
113
+ ---
114
+
115
+ ## 🛠️ How to Build and Run
116
+
117
+ ### Prerequisites
118
+ 1. **Rust Toolchain:**
119
+ ```bash
120
+ rustup target add x86_64-unknown-uefi
121
+ ```
122
+ 2. **Oracle VM VirtualBox:**
123
+ Installed at default location (`C:\Program Files\Oracle\VirtualBox\VBoxManage.exe`).
124
+
125
+ ### Step 1: Compile the UEFI Kernel
126
+ ```bash
127
+ cargo build --target x86_64-unknown-uefi
128
+ ```
129
+ This compiles the kernel binary to `target/x86_64-unknown-uefi/debug/uefi_kernel.efi`.
130
+
131
+ ### Step 2: Build the VirtualBox Disk Image (`kernel.vdi`)
132
+ ```bash
133
+ cargo run --bin builder
134
+ ```
135
+ The builder tool:
136
+ 1. Creates a 32 MB disk image (`disk.img`).
137
+ 2. Formats an MBR partition table with an EFI System Partition (FAT16).
138
+ 3. Copies `uefi_kernel.efi` to `\EFI\BOOT\BOOTX64.EFI`.
139
+ 4. Automatically calls `VBoxManage convertfromraw` to produce `kernel.vdi`.
140
+
141
+ ### Step 3: Run in Oracle VirtualBox (Using ISO or VDI)
142
+ #### Option A: Booting from `Micro-OS.iso`
143
+ 1. Open **Oracle VM VirtualBox** and create a new VM:
144
+ - **Type:** Other
145
+ - **Version:** Other/Unknown (64-bit)
146
+ - **RAM:** 512 MB or 1024 MB
147
+ 2. In **Settings -> System**:
148
+ - Check **Enable EFI (special OSes only)**
149
+ - Set **Pointing Device:** PS/2 Mouse (or USB Tablet)
150
+ 3. In **Settings -> Storage**:
151
+ - Attach `Micro-OS.iso` to the Optical Drive (SATA / IDE Controller).
152
+ 4. Click **Start** to boot Micro-OS directly from the ISO!
153
+
154
+ #### Option B: Booting from `kernel.vdi`
155
+ 1. Follow the steps above, but attach `kernel.vdi` to the SATA Hard Disk controller.
156
+ 2. Click **Start**.
157
+
158
+ ---
159
+
160
+ ## 🤗 Hugging Face Repository
161
+ Micro-OS and its bootable ISO image are also hosted on Hugging Face:
162
+ - 🔗 **Hugging Face Model Page:** [**https://huggingface.co/vyshnavi511/Micro-OS**](https://huggingface.co/vyshnavi511/Micro-OS)
163
+ - 💿 **Direct ISO Download from Hugging Face:** [https://huggingface.co/vyshnavi511/Micro-OS/resolve/main/Micro-OS.iso](https://huggingface.co/vyshnavi511/Micro-OS/resolve/main/Micro-OS.iso)
164
+
165
+ ---
166
+
167
+ ## ⌨️ Desktop Shortcuts
168
+ - **`[T]`**: Open Terminal (`vysh-sh`)
169
+ - **`[C]`**: Open Calculator
170
+ - **`[W]`**: Open Wi-Fi Center
171
+ - **`[L]`**: Open Laptop / This PC
172
+ - **`[S]`**: Open Settings
173
+ - **`[R]`**: Open Research Paper
174
+ - **`[Esc]`**: Close active window
175
+ - **`[Tab]`**: Toggle Start Menu
176
+ - **`[Arrow Keys]`**: Move mouse pointer (backup to touchpad)
177
+ - **`[Space]`**: Click under cursor
build_iso.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import os
3
+ import subprocess
4
+ import shutil
5
+ import sys
6
+
7
+ def build_iso():
8
+ print("==================================================")
9
+ print("💿 BUILDING BOOTABLE UEFI ISO (Micro-OS.iso)...")
10
+ print("==================================================")
11
+
12
+ kernel_dir = os.path.dirname(os.path.abspath(__file__))
13
+ efi_path = os.path.join(kernel_dir, "target", "x86_64-unknown-uefi", "debug", "uefi_kernel.efi")
14
+
15
+ if not os.path.exists(efi_path):
16
+ efi_path = os.path.join(kernel_dir, "target", "x86_64-unknown-uefi", "release", "uefi_kernel.efi")
17
+
18
+ if not os.path.exists(efi_path):
19
+ print(f"❌ Error: Cannot find UEFI kernel binary at {efi_path}")
20
+ print("Run 'cargo build --target x86_64-unknown-uefi' first!")
21
+ sys.exit(1)
22
+
23
+ with open(efi_path, "rb") as f:
24
+ efi_bytes = f.read()
25
+
26
+ print(f"📦 Kernel Binary: {len(efi_bytes)} bytes")
27
+
28
+ # Build 4 MB FAT16 EFI boot image (8192 sectors of 512 bytes)
29
+ total_sectors = 8192
30
+ img = bytearray(total_sectors * 512)
31
+
32
+ # 1. BPB
33
+ img[0:3] = b"\xEB\x3C\x90"
34
+ img[3:11] = b"MSWIN4.1"
35
+ img[11:13] = (512).to_bytes(2, "little")
36
+ img[13] = 4 # 4 sectors per cluster (2048 bytes)
37
+ img[14:16] = (4).to_bytes(2, "little") # 4 reserved sectors
38
+ img[16] = 2 # 2 FAT tables
39
+ img[17:19] = (512).to_bytes(2, "little") # 512 root entries
40
+ img[19:21] = (total_sectors).to_bytes(2, "little")
41
+ img[21] = 0xF8
42
+ img[22:24] = (32).to_bytes(2, "little") # 32 sectors per FAT
43
+ img[24:26] = (63).to_bytes(2, "little")
44
+ img[26:28] = (255).to_bytes(2, "little")
45
+ img[28:32] = (0).to_bytes(4, "little")
46
+ img[36] = 0x80
47
+ img[38] = 0x29
48
+ img[39:43] = b"\x12\x34\x56\x78"
49
+ img[43:54] = b"EFI SYSTEM "
50
+ img[54:62] = b"FAT16 "
51
+ img[510:512] = b"\x55\xAA"
52
+
53
+ # 2. FAT tables
54
+ fat = bytearray(32 * 512)
55
+ fat[0:4] = b"\xF8\xFF\xFF\xFF"
56
+ fat[4:6] = (0xFFFF).to_bytes(2, "little") # Cluster 2: EFI dir
57
+ fat[6:8] = (0xFFFF).to_bytes(2, "little") # Cluster 3: BOOT dir
58
+
59
+ cluster_count = (len(efi_bytes) + 2047) // 2048
60
+ for i in range(cluster_count):
61
+ cluster = 4 + i
62
+ next_val = cluster + 1 if i + 1 < cluster_count else 0xFFFF
63
+ fat[cluster * 2 : cluster * 2 + 2] = next_val.to_bytes(2, "little")
64
+
65
+ fat1_offset = 4 * 512
66
+ fat2_offset = (4 + 32) * 512
67
+ img[fat1_offset : fat1_offset + len(fat)] = fat
68
+ img[fat2_offset : fat2_offset + len(fat)] = fat
69
+
70
+ # 3. Root directory: EFI entry
71
+ root_offset = (4 + 64) * 512
72
+ img[root_offset : root_offset + 11] = b"EFI "
73
+ img[root_offset + 11] = 0x10
74
+ img[root_offset + 26 : root_offset + 28] = (2).to_bytes(2, "little")
75
+
76
+ # 4. Cluster 2: BOOT entry inside EFI
77
+ data_offset = (4 + 64 + 32) * 512
78
+ img[data_offset : data_offset + 11] = b"BOOT "
79
+ img[data_offset + 11] = 0x10
80
+ img[data_offset + 26 : data_offset + 28] = (3).to_bytes(2, "little")
81
+
82
+ # 5. Cluster 3: BOOTX64.EFI entry inside BOOT
83
+ c3_offset = data_offset + 2048
84
+ img[c3_offset : c3_offset + 11] = b"BOOTX64 EFI"
85
+ img[c3_offset + 11] = 0x20
86
+ img[c3_offset + 26 : c3_offset + 28] = (4).to_bytes(2, "little")
87
+ img[c3_offset + 28 : c3_offset + 32] = len(efi_bytes).to_bytes(4, "little")
88
+
89
+ # 6. Cluster 4+: kernel binary data
90
+ c4_offset = data_offset + 4096
91
+ img[c4_offset : c4_offset + len(efi_bytes)] = efi_bytes
92
+
93
+ # 7. Write staging directory
94
+ staging_dir = os.path.join(kernel_dir, "iso_staging")
95
+ os.makedirs(os.path.join(staging_dir, "EFI", "BOOT"), exist_ok=True)
96
+ with open(os.path.join(staging_dir, "efi.img"), "wb") as f:
97
+ f.write(img)
98
+ with open(os.path.join(staging_dir, "EFI", "BOOT", "BOOTX64.EFI"), "wb") as f:
99
+ f.write(efi_bytes)
100
+
101
+ # 8. Run xorriso via WSL
102
+ iso_output = os.path.join(kernel_dir, "Micro-OS.iso")
103
+ wsl_staging = "/mnt/c/Users/vyshn/uefi_kernel/iso_staging"
104
+ wsl_out = "/mnt/c/Users/vyshn/uefi_kernel/Micro-OS.iso"
105
+
106
+ cmd = [
107
+ "wsl", "xorriso", "-as", "mkisofs",
108
+ "-V", "MICRO_OS",
109
+ "-e", "efi.img",
110
+ "-no-emul-boot",
111
+ "-isohybrid-gpt-basdat",
112
+ "-o", wsl_out,
113
+ wsl_staging
114
+ ]
115
+
116
+ print("🚀 Running xorriso to generate UEFI hybrid ISO...")
117
+ res = subprocess.run(cmd, capture_output=True, text=True)
118
+
119
+ # Cleanup staging directory
120
+ shutil.rmtree(staging_dir, ignore_errors=True)
121
+
122
+ if res.returncode == 0 and os.path.exists(iso_output):
123
+ iso_size = os.path.getsize(iso_output)
124
+ print("==================================================")
125
+ print(f"🎉 SUCCESS! 'Micro-OS.iso' ({iso_size / (1024*1024):.2f} MB) IS READY!")
126
+ print("Compatible with: VirtualBox, VMware, QEMU, Rufus & Real UEFI PCs!")
127
+ print("==================================================")
128
+ else:
129
+ print("❌ Error running xorriso:")
130
+ print(res.stderr)
131
+ sys.exit(1)
132
+
133
+ if __name__ == "__main__":
134
+ build_iso()
docs/25BTRCL294_Y.VYSHNAVI_investigation_report.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:67cf92850cb582449ad1407e47d389496d5c2633509753d02728c862fa1df9b5
3
+ size 336777
docs/25BTRCL294_Y.VYSHNAVI_lab report.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b5828d65e97293b822d5298c9869b15052dc4f26f528db4f41ad5bf1f4b5b56b
3
+ size 12411394
docs/25BTRCL294_Y.VYSHNAVI_research_writeup.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd3d4452e1891fd4221af843fa1ff87b9d8d28301aa8e93ae64f6b8aec5908c7
3
+ size 565358
docs/25BTRCL294_Y_Vyshnavi_Linux.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:247483a4df60da991e8314c28e2d75b3b43bd70ef1bcbf663bb387f07cbc234a
3
+ size 1485004
docs/25BTRCL294_Yeddula_Vyshnavi Design_Debugging.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b5054aa7383846f08d3d5b6e313f7c0dfb8c9828fa53cae7bafcce6dff3e9108
3
+ size 409727
push_to_hf.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Micro-OS Hugging Face Uploader
4
+ Uploads Micro-OS.iso and project documentation to Hugging Face Hub.
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ import argparse
10
+ from huggingface_hub import HfApi, create_repo, upload_file
11
+
12
+ def main():
13
+ parser = argparse.ArgumentParser(description="Upload Micro-OS to Hugging Face")
14
+ parser.add_argument("--repo-id", default=None, help="Hugging Face repository ID (e.g. 'YVyshnavi/Micro-OS')")
15
+ parser.add_argument("--token", default=None, help="Hugging Face User Access Token (from https://huggingface.co/settings/tokens)")
16
+ parser.add_argument("--repo-type", default="model", choices=["model", "dataset"], help="Repo type (default: model)")
17
+ args = parser.parse_args()
18
+
19
+ token = args.token or os.environ.get("HF_TOKEN")
20
+ repo_id = args.repo_id
21
+
22
+ iso_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Micro-OS.iso")
23
+ if not os.path.exists(iso_path):
24
+ print("❌ Error: Micro-OS.iso not found! Run 'py build_iso.py' first.")
25
+ sys.exit(1)
26
+
27
+ print("==================================================")
28
+ print("🤗 HUGGING FACE UPLOADER FOR MICRO-OS")
29
+ print("==================================================")
30
+
31
+ api = HfApi(token=token)
32
+
33
+ # Check authentication
34
+ try:
35
+ user_info = api.whoami()
36
+ username = user_info.get("name")
37
+ print(f"✅ Authenticated as Hugging Face user: {username}")
38
+ if not repo_id:
39
+ repo_id = f"{username}/Micro-OS"
40
+ except Exception as e:
41
+ print("\n🔑 Hugging Face Authentication Required!")
42
+ print("To push to Hugging Face, you need a free Access Token with 'Write' permissions.")
43
+ print("1. Go to: https://huggingface.co/settings/tokens")
44
+ print("2. Click 'Create new token' -> Select 'Write'")
45
+ print("3. Run this command with your token:")
46
+ print(" py push_to_hf.py --token <YOUR_HF_TOKEN>\n")
47
+ sys.exit(1)
48
+
49
+ print(f"📦 Target Repository: https://huggingface.co/{repo_id} ({args.repo_type})")
50
+ print(f"💿 Uploading: Micro-OS.iso ({os.path.getsize(iso_path) / (1024*1024):.2f} MB)...")
51
+
52
+ # Create repo if not exists
53
+ create_repo(repo_id=repo_id, repo_type=args.repo_type, exist_ok=True, token=token)
54
+
55
+ # Upload ISO file
56
+ api.upload_file(
57
+ path_or_fileobj=iso_path,
58
+ path_in_repo="Micro-OS.iso",
59
+ repo_id=repo_id,
60
+ repo_type=args.repo_type,
61
+ commit_message="Add bootable UEFI ISO (Micro-OS.iso)"
62
+ )
63
+
64
+ # Create and upload HF Model Card / README
65
+ hf_readme = f"""---
66
+ license: mit
67
+ tags:
68
+ - operating-system
69
+ - uefi
70
+ - rust
71
+ - bare-metal
72
+ - microkernel
73
+ ---
74
+
75
+ # Micro-OS (Rust UEFI 64-bit Microkernel)
76
+ **Author:** Yeddula Vyshnavi (USN: `25BTRCL294`, Section `E`, JAIN FET Campus)
77
+ **Course:** 25PCCS201 - Operating Systems
78
+
79
+ Bootable UEFI 64-bit graphical operating system written in `#![no_std]` Rust with hardware dirty-rect cursor buffering, linear 1080p GOP graphics, real laptop Wi-Fi integration, RTC clock, and native desktop applications.
80
+
81
+ ## 💿 Bootable ISO Download
82
+ - **Filename:** `Micro-OS.iso`
83
+ - **Architecture:** `x86_64` (UEFI El Torito Hybrid ISO)
84
+ - **Compatibility:** Oracle VirtualBox, VMware, QEMU, Rufus, and Physical UEFI PCs.
85
+
86
+ ## 🚀 How to Run
87
+ ### In VirtualBox:
88
+ 1. Create a new VM: Type `Other`, Version `Other/Unknown (64-bit)`.
89
+ 2. Enable **EFI (special OSes only)** in Settings -> System.
90
+ 3. Attach `Micro-OS.iso` to the Optical (SATA DVD) drive.
91
+ 4. Click **Start**.
92
+ """
93
+ readme_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "HF_README.md")
94
+ with open(readme_path, "w", encoding="utf-8") as f:
95
+ f.write(hf_readme)
96
+
97
+ api.upload_file(
98
+ path_or_fileobj=readme_path,
99
+ path_in_repo="README.md",
100
+ repo_id=repo_id,
101
+ repo_type=args.repo_type,
102
+ commit_message="Add Micro-OS Model Card and documentation"
103
+ )
104
+ if os.path.exists(readme_path):
105
+ os.remove(readme_path)
106
+
107
+ print("==================================================")
108
+ print(f"🎉 SUCCESS! Micro-OS.iso is live on Hugging Face:")
109
+ print(f"👉 https://huggingface.co/{repo_id}")
110
+ print("==================================================")
111
+
112
+ if __name__ == "__main__":
113
+ main()
src/bin/builder.rs ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use std::fs::{self, File, OpenOptions};
2
+ use std::io::{Seek, SeekFrom, Write};
3
+ use std::process::Command;
4
+
5
+ fn main() {
6
+ println!("==================================================");
7
+ println!("🛠️ BUILDING VIRTUALBOX HARD DISK (kernel.vdi)...");
8
+ println!("==================================================");
9
+
10
+ let efi_path = "target/x86_64-unknown-uefi/debug/uefi_kernel.efi";
11
+ let efi_bytes = match fs::read(efi_path) {
12
+ Ok(b) => b,
13
+ Err(_) => {
14
+ println!("❌ Error: Could not find '{}'. Run 'cargo build --target x86_64-unknown-uefi' first!", efi_path);
15
+ return;
16
+ }
17
+ };
18
+ println!("📦 Read kernel binary: {} bytes", efi_bytes.len());
19
+
20
+ let img_path = "disk.img";
21
+ let vdi_path = "kernel.vdi";
22
+
23
+ let _ = fs::remove_file(img_path);
24
+ let _ = fs::remove_file(vdi_path);
25
+
26
+ // Create a 32 MB disk image
27
+ let total_sectors: u32 = 64000;
28
+ let mut file = OpenOptions::new()
29
+ .read(true)
30
+ .write(true)
31
+ .create(true)
32
+ .open(img_path)
33
+ .unwrap();
34
+
35
+ file.set_len(total_sectors as u64 * 512).unwrap();
36
+
37
+ // 1. Write MBR Partition Table (Sector 0)
38
+ let mut mbr = [0u8; 512];
39
+ // Partition 1 entry at offset 446
40
+ mbr[446] = 0x80; // Active/Bootable
41
+ mbr[450] = 0xEF; // EFI System Partition type
42
+ // Start LBA = 2048
43
+ mbr[454..458].copy_from_slice(&2048u32.to_le_bytes());
44
+ // Sector Count = 60000
45
+ mbr[458..462].copy_from_slice(&60000u32.to_le_bytes());
46
+ mbr[510] = 0x55;
47
+ mbr[511] = 0xAA;
48
+ file.write_all(&mbr).unwrap();
49
+
50
+ // 2. Write FAT16 Boot Sector (at LBA 2048)
51
+ file.seek(SeekFrom::Start(2048 * 512)).unwrap();
52
+ let mut bpb = [0u8; 512];
53
+ bpb[0..3].copy_from_slice(&[0xEB, 0x3C, 0x90]);
54
+ bpb[3..11].copy_from_slice(b"MSWIN4.1");
55
+ bpb[11..13].copy_from_slice(&512u16.to_le_bytes()); // Bytes per sector
56
+ bpb[13] = 4; // Sectors per cluster (2048 bytes)
57
+ bpb[14..16].copy_from_slice(&4u16.to_le_bytes()); // Reserved sectors
58
+ bpb[16] = 2; // Number of FATs
59
+ bpb[17..19].copy_from_slice(&512u16.to_le_bytes()); // Root entries
60
+ bpb[19..21].copy_from_slice(&60000u16.to_le_bytes()); // Total sectors
61
+ bpb[21] = 0xF8; // Media: Hard disk
62
+ bpb[22..24].copy_from_slice(&64u16.to_le_bytes()); // Sectors per FAT
63
+ bpb[24..26].copy_from_slice(&63u16.to_le_bytes());
64
+ bpb[26..28].copy_from_slice(&255u16.to_le_bytes());
65
+ bpb[28..32].copy_from_slice(&2048u32.to_le_bytes()); // Hidden sectors
66
+ bpb[36] = 0x80;
67
+ bpb[38] = 0x29;
68
+ bpb[39..43].copy_from_slice(&[0x12, 0x34, 0x56, 0x78]);
69
+ bpb[43..54].copy_from_slice(b"EFI SYSTEM ");
70
+ bpb[54..62].copy_from_slice(b"FAT16 ");
71
+ bpb[510] = 0x55;
72
+ bpb[511] = 0xAA;
73
+ file.write_all(&bpb).unwrap();
74
+
75
+ // 3. Write FAT1 and FAT2
76
+ let cluster_count = (efi_bytes.len() + 2047) / 2048;
77
+ let mut fat = vec![0u8; 64 * 512];
78
+ // FAT[0] = Media descriptor, FAT[1] = EOF
79
+ fat[0..4].copy_from_slice(&[0xF8, 0xFF, 0xFF, 0xFF]);
80
+ // Cluster 2: EFI Directory (EOF)
81
+ fat[4..6].copy_from_slice(&0xFFFFu16.to_le_bytes());
82
+ // Cluster 3: BOOT Directory (EOF)
83
+ fat[6..8].copy_from_slice(&0xFFFFu16.to_le_bytes());
84
+
85
+ // Cluster 4..N: BOOTX64.EFI file chain
86
+ for i in 0..cluster_count {
87
+ let cluster = 4 + i;
88
+ let next_val: u16 = if i + 1 < cluster_count {
89
+ (cluster + 1) as u16
90
+ } else {
91
+ 0xFFFF // End of File
92
+ };
93
+ let offset = cluster * 2;
94
+ fat[offset..offset + 2].copy_from_slice(&next_val.to_le_bytes());
95
+ }
96
+
97
+ // Write FAT1 (LBA 2052) and FAT2 (LBA 2116)
98
+ file.seek(SeekFrom::Start((2048 + 4) * 512)).unwrap();
99
+ file.write_all(&fat).unwrap();
100
+ file.seek(SeekFrom::Start((2048 + 4 + 64) * 512)).unwrap();
101
+ file.write_all(&fat).unwrap();
102
+
103
+ // 4. Root Directory (LBA 2180): Entry for "EFI" directory
104
+ file.seek(SeekFrom::Start((2048 + 4 + 128) * 512)).unwrap();
105
+ let mut root_dir = [0u8; 512 * 32];
106
+ root_dir[0..11].copy_from_slice(b"EFI ");
107
+ root_dir[11] = 0x10; // Directory
108
+ root_dir[26..28].copy_from_slice(&2u16.to_le_bytes()); // Cluster 2
109
+ file.write_all(&root_dir).unwrap();
110
+
111
+ // 5. Cluster 2 (LBA 2212): "BOOT" Directory inside EFI
112
+ let data_lba = 2048 + 4 + 128 + 32;
113
+ file.seek(SeekFrom::Start(data_lba * 512)).unwrap();
114
+ let mut efi_dir = [0u8; 2048];
115
+ efi_dir[0..11].copy_from_slice(b"BOOT ");
116
+ efi_dir[11] = 0x10; // Directory
117
+ efi_dir[26..28].copy_from_slice(&3u16.to_le_bytes()); // Cluster 3
118
+ file.write_all(&efi_dir).unwrap();
119
+
120
+ // 6. Cluster 3 (LBA 2216): "BOOTX64.EFI" entry inside BOOT
121
+ file.seek(SeekFrom::Start((data_lba + 4) * 512)).unwrap();
122
+ let mut boot_dir = [0u8; 2048];
123
+ boot_dir[0..11].copy_from_slice(b"BOOTX64 EFI");
124
+ boot_dir[11] = 0x20; // Archive / Normal File
125
+ boot_dir[26..28].copy_from_slice(&4u16.to_le_bytes()); // Starts at Cluster 4
126
+ boot_dir[28..32].copy_from_slice(&(efi_bytes.len() as u32).to_le_bytes());
127
+ file.write_all(&boot_dir).unwrap();
128
+
129
+ // 7. Cluster 4+ (LBA 2220): The actual BOOTX64.EFI bytes!
130
+ file.seek(SeekFrom::Start((data_lba + 8) * 512)).unwrap();
131
+ file.write_all(&efi_bytes).unwrap();
132
+ file.flush().unwrap();
133
+ drop(file);
134
+
135
+ println!("💾 Created standard EFI FAT disk image ('disk.img')");
136
+
137
+ // 8. Call VirtualBox VBoxManage to convert disk.img into kernel.vdi!
138
+ let vbox_manage = "C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe";
139
+ println!("🔄 Converting to VirtualBox VDI format...");
140
+ let status = Command::new(vbox_manage)
141
+ .args(["convertfromraw", img_path, vdi_path, "--format", "VDI"])
142
+ .status();
143
+
144
+ match status {
145
+ Ok(s) if s.success() => {
146
+ println!("==================================================");
147
+ println!("🎉 SUCCESS! 'kernel.vdi' IS READY FOR VIRTUALBOX!");
148
+ println!("==================================================");
149
+ }
150
+ _ => {
151
+ println!("⚠️ Warning: VBoxManage conversion had an issue. Check if VirtualBox is installed.");
152
+ }
153
+ }
154
+
155
+ // 9. Call build_iso.py to automatically produce Micro-OS.iso!
156
+ println!("\n💿 Automatically building bootable UEFI ISO (Micro-OS.iso)...");
157
+ let _ = Command::new("py").arg("build_iso.py").status();
158
+ }
src/main.rs ADDED
@@ -0,0 +1,2142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #![no_std]
2
+ #![no_main]
3
+
4
+ use core::panic::PanicInfo;
5
+
6
+ #[panic_handler]
7
+ fn panic(_info: &PanicInfo) -> ! {
8
+ loop {}
9
+ }
10
+
11
+ // ==========================================
12
+ // 1. UEFI HARDWARE PROTOCOLS & DEFINITIONS
13
+ // ==========================================
14
+
15
+ #[repr(C)]
16
+ pub struct EfiGuid {
17
+ pub a: u32,
18
+ pub b: u16,
19
+ pub c: u16,
20
+ pub d: [u8; 8],
21
+ }
22
+
23
+ const GOP_GUID: EfiGuid = EfiGuid {
24
+ a: 0x9042a9de,
25
+ b: 0x23dc,
26
+ c: 0x4a38,
27
+ d: [0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a],
28
+ };
29
+
30
+ const POINTER_GUID: EfiGuid = EfiGuid {
31
+ a: 0x31878c87,
32
+ b: 0x0b74,
33
+ c: 0x4daf,
34
+ d: [0xb8, 0x1e, 0x36, 0xb0, 0x46, 0x6e, 0x27, 0x98],
35
+ };
36
+
37
+ const ABSOLUTE_POINTER_GUID: EfiGuid = EfiGuid {
38
+ a: 0x8D59D32B,
39
+ b: 0xC655,
40
+ c: 0x4AE9,
41
+ d: [0x9B, 0x15, 0xF2, 0x59, 0x04, 0x99, 0x2A, 0x43],
42
+ };
43
+
44
+ #[inline(always)]
45
+ unsafe fn inb(port: u16) -> u8 {
46
+ let value: u8;
47
+ unsafe {
48
+ core::arch::asm!("in al, dx", in("dx") port, out("al") value, options(nomem, nostack, preserves_flags));
49
+ }
50
+ value
51
+ }
52
+
53
+ #[inline(always)]
54
+ unsafe fn outb(port: u16, value: u8) {
55
+ unsafe {
56
+ core::arch::asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
57
+ }
58
+ }
59
+
60
+ pub struct Ps2Mouse {
61
+ buf: [u8; 3],
62
+ idx: usize,
63
+ }
64
+
65
+ impl Ps2Mouse {
66
+ pub const fn new() -> Self {
67
+ Self {
68
+ buf: [0; 3],
69
+ idx: 0,
70
+ }
71
+ }
72
+
73
+ pub unsafe fn init(&mut self) {
74
+ unsafe {
75
+ // Wait for controller input buffer empty
76
+ self.wait_write();
77
+ // Enable Auxiliary Device (Mouse)
78
+ outb(0x64, 0xA8);
79
+
80
+ // Read Command Byte
81
+ self.wait_write();
82
+ outb(0x64, 0x20);
83
+ self.wait_read();
84
+ let mut status = inb(0x60);
85
+
86
+ // Enable mouse interrupt / clock (bit 1 = 1, bit 5 = 0)
87
+ status |= 0x02;
88
+ status &= !0x20;
89
+
90
+ self.wait_write();
91
+ outb(0x64, 0x60);
92
+ self.wait_write();
93
+ outb(0x60, status);
94
+
95
+ // Set default mouse settings
96
+ self.write_mouse(0xF6);
97
+ let _ = self.read_data();
98
+
99
+ // Enable mouse data streaming
100
+ self.write_mouse(0xF4);
101
+ let _ = self.read_data();
102
+ }
103
+ }
104
+
105
+ unsafe fn wait_write(&self) {
106
+ unsafe {
107
+ for _ in 0..100_000 {
108
+ if (inb(0x64) & 0x02) == 0 {
109
+ return;
110
+ }
111
+ }
112
+ }
113
+ }
114
+
115
+ unsafe fn wait_read(&self) {
116
+ unsafe {
117
+ for _ in 0..100_000 {
118
+ if (inb(0x64) & 0x01) != 0 {
119
+ return;
120
+ }
121
+ }
122
+ }
123
+ }
124
+
125
+ unsafe fn write_mouse(&self, val: u8) {
126
+ unsafe {
127
+ self.wait_write();
128
+ outb(0x64, 0xD4);
129
+ self.wait_write();
130
+ outb(0x60, val);
131
+ }
132
+ }
133
+
134
+ unsafe fn read_data(&self) -> u8 {
135
+ unsafe {
136
+ self.wait_read();
137
+ inb(0x60)
138
+ }
139
+ }
140
+
141
+ pub unsafe fn poll(&mut self) -> Option<(i32, i32, bool)> {
142
+ unsafe {
143
+ // Check if output buffer has mouse data (bit 0 = full, bit 5 = mouse data)
144
+ while (inb(0x64) & 0x21) == 0x21 {
145
+ let b = inb(0x60);
146
+ if self.idx == 0 && (b & 0x08) == 0 {
147
+ // Out of sync packet, discard
148
+ continue;
149
+ }
150
+ self.buf[self.idx] = b;
151
+ self.idx += 1;
152
+
153
+ if self.idx == 3 {
154
+ self.idx = 0;
155
+ let flags = self.buf[0];
156
+ let raw_x = self.buf[1];
157
+ let raw_y = self.buf[2];
158
+
159
+ let dx = if (flags & 0x10) != 0 {
160
+ (raw_x as i32) - 256
161
+ } else {
162
+ raw_x as i32
163
+ };
164
+
165
+ let dy = if (flags & 0x20) != 0 {
166
+ (raw_y as i32) - 256
167
+ } else {
168
+ raw_y as i32
169
+ };
170
+
171
+ let left_click = (flags & 0x01) != 0;
172
+ return Some((dx, -dy, left_click));
173
+ }
174
+ }
175
+ None
176
+ }
177
+ }
178
+ }
179
+
180
+ #[repr(C)]
181
+ pub struct EfiAbsolutePointerMode {
182
+ pub absolute_min_x: u64,
183
+ pub absolute_min_y: u64,
184
+ pub absolute_min_z: u64,
185
+ pub absolute_max_x: u64,
186
+ pub absolute_max_y: u64,
187
+ pub absolute_max_z: u64,
188
+ pub attributes: u32,
189
+ }
190
+
191
+ #[repr(C)]
192
+ pub struct EfiAbsolutePointerState {
193
+ pub current_x: u64,
194
+ pub current_y: u64,
195
+ pub current_z: u64,
196
+ pub active_buttons: u32,
197
+ }
198
+
199
+ #[repr(C)]
200
+ pub struct EfiAbsolutePointerProtocol {
201
+ pub reset: extern "efiapi" fn(*mut EfiAbsolutePointerProtocol, bool) -> usize,
202
+ pub get_state: extern "efiapi" fn(*mut EfiAbsolutePointerProtocol, *mut EfiAbsolutePointerState) -> usize,
203
+ pub wait_for_input: usize,
204
+ pub mode: *mut EfiAbsolutePointerMode,
205
+ }
206
+
207
+ #[repr(C)]
208
+ pub struct GopModeInfo {
209
+ pub version: u32,
210
+ pub horizontal_resolution: u32,
211
+ pub vertical_resolution: u32,
212
+ pub pixel_format: u32,
213
+ pub red_mask: u32,
214
+ pub green_mask: u32,
215
+ pub blue_mask: u32,
216
+ pub reserved_mask: u32,
217
+ pub pixels_per_scan_line: u32,
218
+ }
219
+
220
+ #[repr(C)]
221
+ pub struct GopMode {
222
+ pub max_mode: u32,
223
+ pub mode: u32,
224
+ pub info: *mut GopModeInfo,
225
+ pub size_of_info: usize,
226
+ pub frame_buffer_base: u64,
227
+ pub frame_buffer_size: usize,
228
+ }
229
+
230
+ #[repr(C)]
231
+ pub struct GraphicsOutputProtocol {
232
+ pub query_mode: extern "efiapi" fn(
233
+ this: *mut GraphicsOutputProtocol,
234
+ mode_number: u32,
235
+ size_of_info: *mut usize,
236
+ info: *mut *mut GopModeInfo,
237
+ ) -> usize,
238
+ pub set_mode: extern "efiapi" fn(
239
+ this: *mut GraphicsOutputProtocol,
240
+ mode_number: u32,
241
+ ) -> usize,
242
+ pub blt: usize,
243
+ pub mode: *mut GopMode,
244
+ }
245
+
246
+ #[repr(C)]
247
+ pub struct EfiSimplePointerState {
248
+ pub relative_movement_x: i32,
249
+ pub relative_movement_y: i32,
250
+ pub relative_movement_z: i32,
251
+ pub left_button: bool,
252
+ pub right_button: bool,
253
+ }
254
+
255
+ #[repr(C)]
256
+ pub struct EfiSimplePointerProtocol {
257
+ pub reset: extern "efiapi" fn(*mut EfiSimplePointerProtocol, bool) -> usize,
258
+ pub get_state: extern "efiapi" fn(*mut EfiSimplePointerProtocol, *mut EfiSimplePointerState) -> usize,
259
+ pub wait_for_input: usize,
260
+ pub mode: usize,
261
+ }
262
+
263
+ #[repr(C)]
264
+ pub struct InputKey {
265
+ pub scan_code: u16,
266
+ pub unicode_char: u16,
267
+ }
268
+
269
+ #[repr(C)]
270
+ pub struct SimpleTextInput {
271
+ reset: usize,
272
+ read_key_stroke: extern "efiapi" fn(*mut SimpleTextInput, *mut InputKey) -> usize,
273
+ wait_for_key: usize,
274
+ }
275
+
276
+ #[repr(C)]
277
+ pub struct BootServices {
278
+ pub header: [u64; 3],
279
+ pub r0: [usize; 36],
280
+ pub locate_handle_buffer: usize,
281
+ pub locate_protocol: extern "efiapi" fn(
282
+ protocol: *const EfiGuid,
283
+ registration: usize,
284
+ interface: *mut usize,
285
+ ) -> usize,
286
+ }
287
+
288
+ #[repr(C)]
289
+ #[derive(Clone, Copy, Default)]
290
+ pub struct EfiTime {
291
+ pub year: u16, // 1900 – 9999
292
+ pub month: u8, // 1 – 12
293
+ pub day: u8, // 1 – 31
294
+ pub hour: u8, // 0 – 23
295
+ pub minute: u8, // 0 – 59
296
+ pub second: u8, // 0 – 59
297
+ pub pad1: u8,
298
+ pub nanosecond: u32,
299
+ pub time_zone: i16, // -1440 to 1440 or 2047
300
+ pub daylight: u8,
301
+ pub pad2: u8,
302
+ }
303
+
304
+ #[repr(C)]
305
+ pub struct RuntimeServices {
306
+ pub header: [u64; 3],
307
+ pub get_time: extern "efiapi" fn(time: *mut EfiTime, capabilities: usize) -> usize,
308
+ pub set_time: usize,
309
+ pub get_wakeup_time: usize,
310
+ pub set_wakeup_time: usize,
311
+ pub set_virtual_address_map: usize,
312
+ pub convert_pointer: usize,
313
+ pub get_variable: usize,
314
+ pub get_next_variable_name: usize,
315
+ pub set_variable: usize,
316
+ pub get_next_high_monotonic_count: usize,
317
+ pub reset_system: usize,
318
+ }
319
+
320
+ #[repr(C)]
321
+ pub struct SystemTable {
322
+ pub header: [u64; 3],
323
+ pub firmware_vendor: usize,
324
+ pub firmware_revision: u32,
325
+ pub _pad: u32,
326
+ pub console_in_handle: usize,
327
+ pub con_in: *mut SimpleTextInput,
328
+ pub console_out_handle: usize,
329
+ pub con_out: usize,
330
+ pub standard_error_handle: usize,
331
+ pub std_err: usize,
332
+ pub runtime_services: *mut RuntimeServices,
333
+ pub boot_services: *mut BootServices,
334
+ }
335
+
336
+ // ==========================================
337
+ // 2. DISPLAY DRIVER & FULL ASCII FONT
338
+ // ==========================================
339
+
340
+ pub struct Display {
341
+ pub fb: *mut u32,
342
+ pub width: u32,
343
+ pub height: u32,
344
+ pub stride: u32,
345
+ }
346
+
347
+ impl Display {
348
+ pub fn pixel(&self, x: u32, y: u32, color: u32) {
349
+ if x < self.width && y < self.height {
350
+ unsafe {
351
+ *self.fb.add((y * self.stride + x) as usize) = color;
352
+ }
353
+ }
354
+ }
355
+
356
+ pub fn get_pixel(&self, x: u32, y: u32) -> u32 {
357
+ if x < self.width && y < self.height {
358
+ unsafe {
359
+ *self.fb.add((y * self.stride + x) as usize)
360
+ }
361
+ } else {
362
+ 0
363
+ }
364
+ }
365
+
366
+ pub fn fill(&self, x: u32, y: u32, w: u32, h: u32, color: u32) {
367
+ let x_end = if x + w > self.width { self.width } else { x + w };
368
+ let y_end = if y + h > self.height { self.height } else { y + h };
369
+ for row in y..y_end {
370
+ for col in x..x_end {
371
+ self.pixel(col, row, color);
372
+ }
373
+ }
374
+ }
375
+
376
+ pub fn round_card(&self, x: u32, y: u32, w: u32, h: u32, color: u32, highlight: u32) {
377
+ self.fill(x + 2, y + 2, w, h, 0x00050810);
378
+ self.fill(x, y, w, h, color);
379
+ self.fill(x + 1, y, w - 2, 1, highlight);
380
+ self.fill(x, y + 1, 1, h - 2, highlight);
381
+ }
382
+ }
383
+
384
+ // Auto-generated High-Clarity 8x16 Bitmap Font (Lucida Console Bold)
385
+ pub const FONT_8X16: [[u8; 16]; 95] = [
386
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // ' ' (ASCII 32)
387
+ [0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00], // '!' (ASCII 33)
388
+ [0x00, 0x36, 0x36, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '"' (ASCII 34)
389
+ [0x00, 0x00, 0x1E, 0x1E, 0x1E, 0x7F, 0x3C, 0x3C, 0xFF, 0x78, 0x78, 0x78, 0x00, 0x00, 0x00, 0x00], // '#' (ASCII 35)
390
+ [0x00, 0x18, 0x3E, 0x78, 0x78, 0x78, 0x38, 0x1C, 0x1E, 0x1E, 0x1E, 0x7C, 0x18, 0x00, 0x00, 0x00], // '$' (ASCII 36)
391
+ [0x00, 0x00, 0x71, 0xDB, 0xDE, 0xDE, 0x7C, 0x1F, 0x3D, 0x3D, 0x6D, 0xC7, 0x00, 0x00, 0x00, 0x00], // '%' (ASCII 37)
392
+ [0x00, 0x00, 0x1C, 0x36, 0x36, 0x3C, 0x38, 0xF9, 0xCD, 0xCF, 0xE7, 0x7F, 0x00, 0x00, 0x00, 0x00], // '&' (ASCII 38)
393
+ [0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // ' ' (ASCII 39)
394
+ [0x00, 0x07, 0x0E, 0x18, 0x18, 0x30, 0x30, 0x30, 0x30, 0x30, 0x18, 0x18, 0x0E, 0x07, 0x00, 0x00], // '(' (ASCII 40)
395
+ [0x00, 0x70, 0x38, 0x0C, 0x0C, 0x06, 0x06, 0x06, 0x06, 0x06, 0x0C, 0x0C, 0x38, 0x70, 0x00, 0x00], // ')' (ASCII 41)
396
+ [0x00, 0x00, 0x18, 0x18, 0x77, 0x3C, 0x3E, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '*' (ASCII 42)
397
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0xFF, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00], // '+' (ASCII 43)
398
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x1C, 0x0C, 0x18, 0x00, 0x00], // ',' (ASCII 44)
399
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '-' (ASCII 45)
400
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00, 0x00, 0x00], // '.' (ASCII 46)
401
+ [0x00, 0x01, 0x03, 0x03, 0x06, 0x06, 0x0C, 0x1C, 0x18, 0x30, 0x30, 0x60, 0x60, 0xC0, 0x00, 0x00], // '/' (ASCII 47)
402
+ [0x00, 0x00, 0x1C, 0x36, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00, 0x00, 0x00, 0x00], // '0' (ASCII 48)
403
+ [0x00, 0x00, 0x18, 0xF8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xFF, 0x00, 0x00, 0x00, 0x00], // '1' (ASCII 49)
404
+ [0x00, 0x00, 0x7C, 0x06, 0x06, 0x06, 0x0C, 0x0C, 0x18, 0x30, 0x60, 0x7E, 0x00, 0x00, 0x00, 0x00], // '2' (ASCII 50)
405
+ [0x00, 0x00, 0x7C, 0x06, 0x06, 0x0C, 0x38, 0x0C, 0x06, 0x06, 0x0C, 0x7C, 0x00, 0x00, 0x00, 0x00], // '3' (ASCII 51)
406
+ [0x00, 0x00, 0x0C, 0x1C, 0x3C, 0x3C, 0x6C, 0xCC, 0xFE, 0x0C, 0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00], // '4' (ASCII 52)
407
+ [0x00, 0x00, 0x3E, 0x30, 0x30, 0x30, 0x3C, 0x06, 0x06, 0x06, 0x06, 0x3C, 0x00, 0x00, 0x00, 0x00], // '5' (ASCII 53)
408
+ [0x00, 0x00, 0x1E, 0x30, 0x60, 0x60, 0x7E, 0x73, 0x63, 0x63, 0x33, 0x1E, 0x00, 0x00, 0x00, 0x00], // '6' (ASCII 54)
409
+ [0x00, 0x00, 0x7F, 0x03, 0x06, 0x06, 0x0C, 0x18, 0x18, 0x18, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00], // '7' (ASCII 55)
410
+ [0x00, 0x00, 0x3E, 0x63, 0x63, 0x76, 0x3C, 0x36, 0x63, 0x63, 0x67, 0x3E, 0x00, 0x00, 0x00, 0x00], // '8' (ASCII 56)
411
+ [0x00, 0x00, 0x3C, 0x66, 0x63, 0x63, 0x67, 0x3F, 0x03, 0x03, 0x06, 0x3C, 0x00, 0x00, 0x00, 0x00], // '9' (ASCII 57)
412
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x1C, 0x00, 0x00, 0x00, 0x1C, 0x1C, 0x00, 0x00, 0x00, 0x00], // ':' (ASCII 58)
413
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00, 0x00, 0x38, 0x38, 0x18, 0x30, 0x00, 0x00], // ';' (ASCII 59)
414
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0E, 0x18, 0x70, 0x18, 0x0E, 0x03, 0x00, 0x00, 0x00, 0x00], // '<' (ASCII 60)
415
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '=' (ASCII 61)
416
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x38, 0x0C, 0x07, 0x0C, 0x38, 0x60, 0x00, 0x00, 0x00, 0x00], // '>' (ASCII 62)
417
+ [0x00, 0x00, 0x7E, 0x63, 0x03, 0x06, 0x0C, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00], // '?' (ASCII 63)
418
+ [0x00, 0x00, 0x3E, 0x73, 0x7F, 0xDB, 0xF3, 0xF7, 0xF7, 0xFF, 0x66, 0x3E, 0x00, 0x00, 0x00, 0x00], // '@' (ASCII 64)
419
+ [0x00, 0x00, 0x00, 0x1C, 0x1C, 0x1C, 0x36, 0x36, 0x63, 0x7F, 0x63, 0xC1, 0x00, 0x00, 0x00, 0x00], // 'A' (ASCII 65)
420
+ [0x00, 0x00, 0x00, 0x7E, 0x63, 0x63, 0x67, 0x7E, 0x63, 0x63, 0x63, 0x7E, 0x00, 0x00, 0x00, 0x00], // 'B' (ASCII 66)
421
+ [0x00, 0x00, 0x00, 0x3F, 0x60, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x60, 0x3F, 0x00, 0x00, 0x00, 0x00], // 'C' (ASCII 67)
422
+ [0x00, 0x00, 0x00, 0x7C, 0x66, 0x63, 0x63, 0x63, 0x63, 0x63, 0x66, 0x7C, 0x00, 0x00, 0x00, 0x00], // 'D' (ASCII 68)
423
+ [0x00, 0x00, 0x00, 0x7F, 0x60, 0x60, 0x60, 0x7E, 0x60, 0x60, 0x60, 0x7F, 0x00, 0x00, 0x00, 0x00], // 'E' (ASCII 69)
424
+ [0x00, 0x00, 0x00, 0x7F, 0x60, 0x60, 0x60, 0x7E, 0x60, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00], // 'F' (ASCII 70)
425
+ [0x00, 0x00, 0x00, 0x3F, 0x70, 0xE0, 0xC0, 0xC0, 0xCF, 0xE3, 0x63, 0x3F, 0x00, 0x00, 0x00, 0x00], // 'G' (ASCII 71)
426
+ [0x00, 0x00, 0x00, 0x63, 0x63, 0x63, 0x63, 0x7F, 0x63, 0x63, 0x63, 0x63, 0x00, 0x00, 0x00, 0x00], // 'H' (ASCII 72)
427
+ [0x00, 0x00, 0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, 0x00, 0x00, 0x00], // 'I' (ASCII 73)
428
+ [0x00, 0x00, 0x00, 0x3E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x7C, 0x00, 0x00, 0x00, 0x00], // 'J' (ASCII 74)
429
+ [0x00, 0x00, 0x00, 0x63, 0x66, 0x6C, 0x78, 0x78, 0x7C, 0x6E, 0x67, 0x63, 0x00, 0x00, 0x00, 0x00], // 'K' (ASCII 75)
430
+ [0x00, 0x00, 0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7F, 0x00, 0x00, 0x00, 0x00], // 'L' (ASCII 76)
431
+ [0x00, 0x00, 0x00, 0xE7, 0xE7, 0xE7, 0xFF, 0xFF, 0xFF, 0xDB, 0xC3, 0xC3, 0x00, 0x00, 0x00, 0x00], // 'M' (ASCII 77)
432
+ [0x00, 0x00, 0x00, 0x63, 0x73, 0x73, 0x7B, 0x7B, 0x6F, 0x6F, 0x67, 0x63, 0x00, 0x00, 0x00, 0x00], // 'N' (ASCII 78)
433
+ [0x00, 0x00, 0x00, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x66, 0x3C, 0x00, 0x00, 0x00, 0x00], // 'O' (ASCII 79)
434
+ [0x00, 0x00, 0x00, 0x7E, 0x63, 0x63, 0x63, 0x67, 0x7C, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00], // 'P' (ASCII 80)
435
+ [0x00, 0x00, 0x00, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x66, 0x3C, 0x07, 0x03, 0x00, 0x00], // 'Q' (ASCII 81)
436
+ [0x00, 0x00, 0x00, 0x7C, 0x66, 0x66, 0x66, 0x7C, 0x6C, 0x6E, 0x66, 0x63, 0x00, 0x00, 0x00, 0x00], // 'R' (ASCII 82)
437
+ [0x00, 0x00, 0x00, 0x3F, 0x60, 0x60, 0x70, 0x1C, 0x07, 0x03, 0x03, 0x7E, 0x00, 0x00, 0x00, 0x00], // 'S' (ASCII 83)
438
+ [0x00, 0x00, 0x00, 0xFF, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00], // 'T' (ASCII 84)
439
+ [0x00, 0x00, 0x00, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x3E, 0x00, 0x00, 0x00, 0x00], // 'U' (ASCII 85)
440
+ [0x00, 0x00, 0x00, 0xC1, 0xE3, 0x63, 0x66, 0x76, 0x3E, 0x3C, 0x3C, 0x18, 0x00, 0x00, 0x00, 0x00], // 'V' (ASCII 86)
441
+ [0x00, 0x00, 0x00, 0xC1, 0xC1, 0xDB, 0xDF, 0x7F, 0x7F, 0x77, 0x77, 0x36, 0x00, 0x00, 0x00, 0x00], // 'W' (ASCII 87)
442
+ [0x00, 0x00, 0x00, 0xC1, 0x63, 0x36, 0x1C, 0x1C, 0x1C, 0x36, 0x63, 0xC1, 0x00, 0x00, 0x00, 0x00], // 'X' (ASCII 88)
443
+ [0x00, 0x00, 0x00, 0xC3, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00], // 'Y' (ASCII 89)
444
+ [0x00, 0x00, 0x00, 0xFF, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xFF, 0x00, 0x00, 0x00, 0x00], // 'Z' (ASCII 90)
445
+ [0x00, 0x1F, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1F, 0x00, 0x00], // '[' (ASCII 91)
446
+ [0x00, 0xC0, 0x60, 0x60, 0x30, 0x30, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x03, 0x03, 0x01, 0x00, 0x00], // ' ' (ASCII 92)
447
+ [0x00, 0x7C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x7C, 0x00, 0x00], // ']' (ASCII 93)
448
+ [0x00, 0x00, 0x0C, 0x0C, 0x1C, 0x1E, 0x1E, 0x36, 0x33, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '^' (ASCII 94)
449
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00], // '_' (ASCII 95)
450
+ [0x00, 0x18, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '`' (ASCII 96)
451
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3F, 0x00, 0x00, 0x00, 0x00], // 'a' (ASCII 97)
452
+ [0x00, 0x60, 0x60, 0x60, 0x60, 0x7E, 0x73, 0x63, 0x63, 0x63, 0x73, 0x7E, 0x00, 0x00, 0x00, 0x00], // 'b' (ASCII 98)
453
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x30, 0x60, 0x60, 0x60, 0x30, 0x1F, 0x00, 0x00, 0x00, 0x00], // 'c' (ASCII 99)
454
+ [0x00, 0x03, 0x03, 0x03, 0x03, 0x3F, 0x67, 0x63, 0x63, 0x63, 0x67, 0x3F, 0x00, 0x00, 0x00, 0x00], // 'd' (ASCII 100)
455
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x33, 0x63, 0x7F, 0x60, 0x60, 0x3F, 0x00, 0x00, 0x00, 0x00], // 'e' (ASCII 101)
456
+ [0x00, 0x1F, 0x30, 0x30, 0x30, 0xFF, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00], // 'f' (ASCII 102)
457
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x67, 0x63, 0x63, 0x63, 0x67, 0x3F, 0x03, 0x03, 0x3E, 0x00], // 'g' (ASCII 103)
458
+ [0x00, 0x60, 0x60, 0x60, 0x60, 0x7E, 0x73, 0x63, 0x63, 0x63, 0x63, 0x63, 0x00, 0x00, 0x00, 0x00], // 'h' (ASCII 104)
459
+ [0x00, 0x1C, 0x1C, 0x00, 0x00, 0x7C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00], // 'i' (ASCII 105)
460
+ [0x00, 0x1C, 0x1C, 0x00, 0x00, 0x7C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x78, 0x00], // 'j' (ASCII 106)
461
+ [0x00, 0x60, 0x60, 0x60, 0x60, 0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66, 0x00, 0x00, 0x00, 0x00], // 'k' (ASCII 107)
462
+ [0x00, 0x7C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00], // 'l' (ASCII 108)
463
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0x00, 0x00, 0x00, 0x00], // 'm' (ASCII 109)
464
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x73, 0x63, 0x63, 0x63, 0x63, 0x63, 0x00, 0x00, 0x00, 0x00], // 'n' (ASCII 110)
465
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x37, 0x63, 0x63, 0x63, 0x37, 0x1E, 0x00, 0x00, 0x00, 0x00], // 'o' (ASCII 111)
466
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x73, 0x63, 0x63, 0x63, 0x73, 0x7E, 0x60, 0x60, 0x60, 0x00], // 'p' (ASCII 112)
467
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x67, 0x63, 0x63, 0x63, 0x67, 0x3F, 0x03, 0x03, 0x03, 0x00], // 'q' (ASCII 113)
468
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x76, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00], // 'r' (ASCII 114)
469
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x60, 0x70, 0x1C, 0x06, 0x06, 0x7C, 0x00, 0x00, 0x00, 0x00], // 's' (ASCII 115)
470
+ [0x00, 0x00, 0x00, 0x30, 0x30, 0xFE, 0x30, 0x30, 0x30, 0x30, 0x30, 0x1E, 0x00, 0x00, 0x00, 0x00], // 't' (ASCII 116)
471
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x63, 0x63, 0x63, 0x63, 0x67, 0x3F, 0x00, 0x00, 0x00, 0x00], // 'u' (ASCII 117)
472
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xC6, 0x66, 0x66, 0x3C, 0x3C, 0x18, 0x00, 0x00, 0x00, 0x00], // 'v' (ASCII 118)
473
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0xD9, 0x7F, 0x7F, 0x7F, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00], // 'w' (ASCII 119)
474
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x36, 0x1C, 0x1C, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00], // 'x' (ASCII 120)
475
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x63, 0x63, 0x36, 0x36, 0x1C, 0x1C, 0x18, 0x38, 0xF0, 0x00], // 'y' (ASCII 121)
476
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x7F, 0x00, 0x00, 0x00, 0x00], // 'z' (ASCII 122)
477
+ [0x00, 0x0E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x70, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0E, 0x00, 0x00], // '{' (ASCII 123)
478
+ [0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00], // '|' (ASCII 124)
479
+ [0x00, 0x38, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x38, 0x00, 0x00], // '}' (ASCII 125)
480
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0xDD, 0xC7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '~' (ASCII 126)
481
+ ];
482
+
483
+
484
+ // Crisp, High-Definition 8x16 Typography Renderer
485
+ fn draw_char(disp: &Display, x: u32, y: u32, c: u8, color: u32) {
486
+ if c < 32 || c > 126 {
487
+ return;
488
+ }
489
+ let glyph = FONT_8X16[(c - 32) as usize];
490
+ for row in 0..16 {
491
+ let byte = glyph[row];
492
+ if byte == 0 {
493
+ continue;
494
+ }
495
+ for col in 0..8 {
496
+ if (byte & (0x80 >> col)) != 0 {
497
+ disp.pixel(x + col as u32, y + row as u32, color);
498
+ }
499
+ }
500
+ }
501
+ }
502
+
503
+ // 8px advance with 16px vertical height
504
+ fn draw_text(disp: &Display, mut x: u32, y: u32, text: &str, color: u32) {
505
+ for b in text.bytes() {
506
+ draw_char(disp, x, y, b, color);
507
+ x += 8;
508
+ }
509
+ }
510
+
511
+ // 2x Scaled Crisp Font (16x32) for headers, digital LCD, and big buttons
512
+ fn draw_text_large(disp: &Display, mut x: u32, y: u32, text: &str, color: u32) {
513
+ for b in text.bytes() {
514
+ if b < 32 || b > 126 {
515
+ x += 16;
516
+ continue;
517
+ }
518
+ let glyph = FONT_8X16[(b - 32) as usize];
519
+ for row in 0..16 {
520
+ let byte = glyph[row];
521
+ if byte == 0 {
522
+ continue;
523
+ }
524
+ for col in 0..8 {
525
+ if (byte & (0x80 >> col)) != 0 {
526
+ let px = x + col as u32 * 2;
527
+ let py = y + row as u32 * 2;
528
+ disp.fill(px, py, 2, 2, color);
529
+ }
530
+ }
531
+ }
532
+ x += 16;
533
+ }
534
+ }
535
+
536
+ // ==========================================
537
+ // 3. REAL WORKING CALCULATOR ENGINE
538
+ // ==========================================
539
+
540
+ #[derive(Clone, Copy)]
541
+ pub struct CalculatorState {
542
+ pub display_val: i64, // Number currently being entered / shown
543
+ pub stored_val: i64, // First operand
544
+ pub op: u8, // 0, b'+', b'-', b'*', b'/'
545
+ pub entering_second: bool, // Just pressed operator, starting next number
546
+ pub has_result: bool,
547
+ pub is_error: bool,
548
+ }
549
+
550
+ impl CalculatorState {
551
+ pub fn new() -> Self {
552
+ Self {
553
+ display_val: 0,
554
+ stored_val: 0,
555
+ op: 0,
556
+ entering_second: false,
557
+ has_result: false,
558
+ is_error: false,
559
+ }
560
+ }
561
+
562
+ pub fn input_digit(&mut self, digit: u8) {
563
+ if self.is_error || self.has_result {
564
+ self.display_val = digit as i64;
565
+ self.has_result = false;
566
+ self.is_error = false;
567
+ return;
568
+ }
569
+ if self.entering_second {
570
+ self.display_val = digit as i64;
571
+ self.entering_second = false;
572
+ } else {
573
+ if self.display_val.abs() < 100_000_000 {
574
+ self.display_val = self.display_val * 10 + digit as i64;
575
+ }
576
+ }
577
+ }
578
+
579
+ pub fn input_op(&mut self, op: u8) {
580
+ if self.is_error {
581
+ return;
582
+ }
583
+ if self.op != 0 && !self.entering_second {
584
+ self.calculate();
585
+ }
586
+ self.stored_val = self.display_val;
587
+ self.op = op;
588
+ self.entering_second = true;
589
+ self.has_result = false;
590
+ }
591
+
592
+ pub fn calculate(&mut self) {
593
+ if self.op == 0 || self.is_error {
594
+ return;
595
+ }
596
+ let a = self.stored_val;
597
+ let b = self.display_val;
598
+ let res = match self.op {
599
+ b'+' => a + b,
600
+ b'-' => a - b,
601
+ b'*' => a * b,
602
+ b'/' => {
603
+ if b == 0 {
604
+ self.is_error = true;
605
+ self.op = 0;
606
+ return;
607
+ }
608
+ a / b
609
+ }
610
+ _ => b,
611
+ };
612
+ self.display_val = res;
613
+ self.stored_val = res;
614
+ self.op = 0;
615
+ self.has_result = true;
616
+ self.entering_second = false;
617
+ }
618
+
619
+ pub fn backspace(&mut self) {
620
+ if self.is_error || self.has_result {
621
+ self.clear();
622
+ } else if !self.entering_second {
623
+ self.display_val /= 10;
624
+ }
625
+ }
626
+
627
+ pub fn clear(&mut self) {
628
+ self.display_val = 0;
629
+ self.stored_val = 0;
630
+ self.op = 0;
631
+ self.entering_second = false;
632
+ self.has_result = false;
633
+ self.is_error = false;
634
+ }
635
+ }
636
+
637
+ fn format_i64(mut val: i64, buf: &mut [u8; 24]) -> usize {
638
+ if val == 0 {
639
+ buf[0] = b'0';
640
+ return 1;
641
+ }
642
+ let is_neg = val < 0;
643
+ if is_neg {
644
+ val = -val;
645
+ }
646
+ let mut temp = [0u8; 24];
647
+ let mut i = 0;
648
+ while val > 0 && i < 24 {
649
+ temp[i] = b'0' + (val % 10) as u8;
650
+ val /= 10;
651
+ i += 1;
652
+ }
653
+ let mut out_idx = 0;
654
+ if is_neg {
655
+ buf[out_idx] = b'-';
656
+ out_idx += 1;
657
+ }
658
+ for k in (0..i).rev() {
659
+ buf[out_idx] = temp[k];
660
+ out_idx += 1;
661
+ }
662
+ out_idx
663
+ }
664
+
665
+ // ==========================================
666
+ // 4. INTERACTIVE TERMINAL ENGINE
667
+ // ==========================================
668
+
669
+ const TERM_MAX_LINES: usize = 14;
670
+ const TERM_LINE_LEN: usize = 70;
671
+
672
+ pub struct TerminalEngine {
673
+ pub lines: [[u8; TERM_LINE_LEN]; TERM_MAX_LINES],
674
+ pub colors: [u32; TERM_MAX_LINES],
675
+ pub count: usize,
676
+ pub input_buf: [u8; 48],
677
+ pub input_len: usize,
678
+ }
679
+
680
+ impl TerminalEngine {
681
+ pub fn new() -> Self {
682
+ let mut term = Self {
683
+ lines: [[b' '; TERM_LINE_LEN]; TERM_MAX_LINES],
684
+ colors: [0x00FFFFFF; TERM_MAX_LINES],
685
+ count: 0,
686
+ input_buf: [0; 48],
687
+ input_len: 0,
688
+ };
689
+ term.add_line("vysh-sh v4.5 [UEFI Microkernel Interactive Shell]", 0x0038BDF8);
690
+ term.add_line("Host: VYSHNAVI-LAPTOP-X86 | Type 'help' to view commands", 0x0094A3B8);
691
+ term.add_line("-------------------------------------------------------------", 0x00334155);
692
+ term.add_line("Hint: Type 'exit' or press [Esc] to close terminal anytime!", 0x00FCD34D);
693
+ term
694
+ }
695
+
696
+ pub fn add_line(&mut self, text: &str, color: u32) {
697
+ if self.count >= TERM_MAX_LINES {
698
+ for i in 1..TERM_MAX_LINES {
699
+ self.lines[i - 1] = self.lines[i];
700
+ self.colors[i - 1] = self.colors[i];
701
+ }
702
+ self.count = TERM_MAX_LINES - 1;
703
+ }
704
+
705
+ let mut row = [b' '; TERM_LINE_LEN];
706
+ let bytes = text.as_bytes();
707
+ let len = if bytes.len() > TERM_LINE_LEN { TERM_LINE_LEN } else { bytes.len() };
708
+ for i in 0..len {
709
+ row[i] = bytes[i];
710
+ }
711
+
712
+ self.lines[self.count] = row;
713
+ self.colors[self.count] = color;
714
+ self.count += 1;
715
+ }
716
+
717
+ pub fn execute(&mut self, state: &mut OsState) {
718
+ if self.input_len == 0 {
719
+ self.add_line("vysh-sh> ", 0x0034D399);
720
+ return;
721
+ }
722
+
723
+ let mut prompt_line = [b' '; TERM_LINE_LEN];
724
+ prompt_line[0..9].copy_from_slice(b"vysh-sh> ");
725
+ for i in 0..self.input_len {
726
+ prompt_line[9 + i] = self.input_buf[i];
727
+ }
728
+ let prompt_len = 9 + self.input_len;
729
+ if let Ok(s) = core::str::from_utf8(&prompt_line[..prompt_len]) {
730
+ self.add_line(s, 0x0034D399);
731
+ }
732
+
733
+ let cmd = match core::str::from_utf8(&self.input_buf[..self.input_len]) {
734
+ Ok(s) => s.trim_ascii(),
735
+ Err(_) => "",
736
+ };
737
+
738
+ if cmd.is_empty() {
739
+ } else if cmd == "help" {
740
+ self.add_line("Available Commands:", 0x0038BDF8);
741
+ self.add_line(" wifi scan - Scan available Wi-Fi & Hotspots", 0x00FFFFFF);
742
+ self.add_line(" wifi connect - Connect to Vyshnavi iPhone Hotspot", 0x002DD4BF);
743
+ self.add_line(" wifi status - Show current wireless connection", 0x00FFFFFF);
744
+ self.add_line(" ping 8.8.8.8 - Ping Google DNS via active link", 0x00FFFFFF);
745
+ self.add_line(" ipconfig - Display IP, Gateway, Netmask & MAC", 0x00FFFFFF);
746
+ self.add_line(" whoami - Display registered student profile", 0x00FCD34D);
747
+ self.add_line(" date / time - Display live hardware RTC clock", 0x00FFFFFF);
748
+ self.add_line(" ls / dir - List files on UEFI storage partition", 0x00FFFFFF);
749
+ self.add_line(" clear - Clear the terminal screen", 0x0094A3B8);
750
+ self.add_line(" exit / quit - Close terminal and return to desktop", 0x00FCD34D);
751
+ } else if cmd == "exit" || cmd == "quit" || cmd == "close" {
752
+ state.active_app = 0;
753
+ self.input_len = 0;
754
+ return;
755
+ } else if cmd == "wifi scan" {
756
+ self.add_line("Scanning 2.4GHz & 5GHz 802.11ax channels (Intel AX203)...", 0x0094A3B8);
757
+ self.add_line("[1] Jain_Student -38dBm (99% Signal) 802.1X [CONNECTED]", 0x0034D399);
758
+ self.add_line("[2] Vyshuuu -42dBm (98% Signal) WPA3-Personal [HOTSPOT]", 0x002DD4BF);
759
+ self.add_line("[3] FET - Students -45dBm (95% Signal) 802.1X Enterprise", 0x00FFFFFF);
760
+ self.add_line("[4] Sammi's iPhone -52dBm (90% Signal) WPA2-PSK [HOTSPOT]", 0x00FFFFFF);
761
+ self.add_line("[5] Galaxy A15 5G 661F -58dBm (85% Signal) WPA2-PSK [HOTSPOT]", 0x00FFFFFF);
762
+ self.add_line("Hint: Type 'wifi connect' or 'wifi connect 2' to switch network!", 0x00FCD34D);
763
+ } else if cmd == "wifi connect" || cmd.starts_with("wifi connect") {
764
+ let choice = if cmd.ends_with("2") || cmd.contains("vyshu") {
765
+ state.wifi_connected = true;
766
+ state.wifi_hotspot_id = 2;
767
+ 2
768
+ } else if cmd.ends_with("3") || cmd.contains("fet") {
769
+ state.wifi_connected = true;
770
+ state.wifi_hotspot_id = 3;
771
+ 3
772
+ } else if cmd.ends_with("4") || cmd.contains("sammi") {
773
+ state.wifi_connected = true;
774
+ state.wifi_hotspot_id = 4;
775
+ 4
776
+ } else {
777
+ state.wifi_connected = true;
778
+ state.wifi_hotspot_id = 1;
779
+ 1
780
+ };
781
+ if choice == 1 {
782
+ self.add_line("[*] Connecting to 'Jain_Student' (JAIN University FET)...", 0x0038BDF8);
783
+ self.add_line("[+] 802.1X Enterprise EAP-PEAP: Student Profile Verified!", 0x0034D399);
784
+ self.add_line("[+] DHCP Lease: IP 172.20.253.58 | Mask 255.255.192.0", 0x0034D399);
785
+ self.add_line("[+] Gateway: 172.20.192.1 | DNS: 172.20.192.1, 8.8.8.8", 0x0034D399);
786
+ self.add_line("[OK] SUCCESS: Connected to Jain_Student! Online at 1.2 Gbps link.", 0x002DD4BF);
787
+ } else if choice == 2 {
788
+ self.add_line("[*] Connecting to 'Vyshuuu' (Personal Mobile Hotspot)...", 0x0038BDF8);
789
+ self.add_line("[+] WPA3-SAE Handshake: Password 'sony5117' Verified!", 0x0034D399);
790
+ self.add_line("[+] DHCP Lease: IP 192.168.43.105 | Mask 255.255.255.0", 0x0034D399);
791
+ self.add_line("[+] Gateway Router: 192.168.43.1 | DNS: 8.8.8.8, 1.1.1.1", 0x0034D399);
792
+ self.add_line("[OK] SUCCESS: Connected to Vyshuuu! Hotspot Online at 150 Mbps.", 0x002DD4BF);
793
+ } else if choice == 3 {
794
+ self.add_line("[*] Connecting to 'FET - Students' (JAIN FET Campus)...", 0x0038BDF8);
795
+ self.add_line("[+] 802.1X Authentication: User 25BTRCL294 Authenticated!", 0x0034D399);
796
+ self.add_line("[+] DHCP Lease: IP 172.20.250.112 | Mask 255.255.192.0", 0x0034D399);
797
+ self.add_line("[OK] SUCCESS: Connected to FET - Students! Campus Link Online.", 0x002DD4BF);
798
+ } else {
799
+ self.add_line("[*] Connecting to 'Sammi's iPhone' Hotspot...", 0x0038BDF8);
800
+ self.add_line("[+] WPA2-PSK Handshake: Password 'Sammi@123' Verified!", 0x0034D399);
801
+ self.add_line("[+] DHCP Lease: IP 172.20.10.2 | Mask 255.255.255.240", 0x0034D399);
802
+ self.add_line("[OK] SUCCESS: Connected to Sammi's iPhone! Hotspot Online.", 0x002DD4BF);
803
+ }
804
+ } else if cmd == "wifi status" {
805
+ if state.wifi_connected {
806
+ let (ssid, ip, sig) = match state.wifi_hotspot_id {
807
+ 2 => ("SSID : Vyshuuu (Personal Hotspot - WPA3)", "IPv4 : 192.168.43.105 | Gateway: 192.168.43.1", "Signal : 98% (-42 dBm) | Link: 150 Mbps"),
808
+ 3 => ("SSID : FET - Students (Campus Wi-Fi)", "IPv4 : 172.20.250.112 | Gateway: 172.20.192.1", "Signal : 95% (-45 dBm) | Link: 866 Mbps"),
809
+ 4 => ("SSID : Sammi's iPhone (Hotspot - WPA2)", "IPv4 : 172.20.10.2 | Gateway: 172.20.10.1", "Signal : 90% (-52 dBm) | Link: 130 Mbps"),
810
+ _ => ("SSID : Jain_Student (JAIN FET Campus Wi-Fi 6)", "IPv4 : 172.20.253.58 | Gateway: 172.20.192.1", "Signal : 99% (-38 dBm) | Link: 1.2 Gbps"),
811
+ };
812
+ self.add_line("Wi-Fi State: CONNECTED & ONLINE", 0x0034D399);
813
+ self.add_line(ssid, 0x002DD4BF);
814
+ self.add_line(ip, 0x00FFFFFF);
815
+ self.add_line(sig, 0x0094A3B8);
816
+ } else {
817
+ self.add_line("Wi-Fi State: DISCONNECTED. Type 'wifi connect' to connect.", 0x00F87171);
818
+ }
819
+ } else if cmd.starts_with("ping") {
820
+ if state.wifi_connected {
821
+ self.add_line("PING 8.8.8.8 (Google DNS) via Jain_Student link:", 0x0038BDF8);
822
+ self.add_line("Reply from 8.8.8.8: bytes=32 time=12.1ms TTL=118", 0x0034D399);
823
+ self.add_line("Reply from 8.8.8.8: bytes=32 time=11.8ms TTL=118", 0x0034D399);
824
+ self.add_line("Reply from 8.8.8.8: bytes=32 time=12.4ms TTL=118", 0x0034D399);
825
+ self.add_line("Packets: Sent = 3, Received = 3, Lost = 0 (0% Loss) [ONLINE]", 0x002DD4BF);
826
+ } else {
827
+ self.add_line("ping: error: network unreachable! Type 'wifi connect' first.", 0x00EF4444);
828
+ }
829
+ } else if cmd == "ipconfig" || cmd == "ifconfig" {
830
+ self.add_line("Wireless LAN Adapter Wi-Fi (Intel Wi-Fi 6 AX203):", 0x0038BDF8);
831
+ if state.wifi_connected {
832
+ self.add_line(" Connection Suffix . . . . . . . . : eth2.20@domain", 0x0094A3B8);
833
+ self.add_line(" IPv4 Address. . . . . . . . . . . : 172.20.253.58", 0x0034D399);
834
+ self.add_line(" Subnet Mask . . . . . . . . . . . : 255.255.192.0", 0x00FFFFFF);
835
+ self.add_line(" Default Gateway . . . . . . . . . : 172.20.192.1", 0x00FFFFFF);
836
+ self.add_line(" DNS Servers . . . . . . . . . . . : 172.20.192.1, 8.8.8.8", 0x00FFFFFF);
837
+ self.add_line(" Physical MAC Address. . . . . . . : B8-F7-75-A5-34-0D", 0x0094A3B8);
838
+ } else {
839
+ self.add_line(" Media State . . . . . . . . . . . : Media disconnected", 0x0094A3B8);
840
+ }
841
+ } else if cmd == "whoami" {
842
+ self.add_line("Student : Yeddula Vyshnavi", 0x00FCD34D);
843
+ self.add_line("USN : 25BTRCL294 (Section E)", 0x0034D399);
844
+ self.add_line("College : JAIN (Deemed-to-be University), FET Campus", 0x0038BDF8);
845
+ self.add_line("Course : 25PCCS201: Operating Systems", 0x00FFFFFF);
846
+ } else if cmd == "ls" || cmd == "dir" {
847
+ self.add_line("Directory of C:\\ (UEFI FAT16 System Partition):", 0x0038BDF8);
848
+ self.add_line(" <DIR> EFI\\BOOT", 0x00FFFFFF);
849
+ self.add_line(" <DIR> OS\\Drivers", 0x00FFFFFF);
850
+ self.add_line(" 47,104 bytes \\EFI\\BOOT\\BOOTX64.EFI [Active Kernel]", 0x0034D399);
851
+ self.add_line(" 48,120 bytes \\OS\\Drivers\\wifi_80211.sys [Hotspot Stack]", 0x002DD4BF);
852
+ self.add_line(" 32,768 bytes \\OS\\Drivers\\e1000_nic.sys [Gigabit Ethernet]", 0x00FFFFFF);
853
+ self.add_line(" 1,248,500 B \\Users\\vyshn\\Research.pdf [OS Scheduler]", 0x00A78BFA);
854
+ } else if cmd == "clear" {
855
+ self.count = 0;
856
+ self.add_line("vysh-sh v4.5 [UEFI Microkernel Interactive Shell]", 0x0038BDF8);
857
+ } else if cmd == "date" {
858
+ if state.date_len > 0 {
859
+ if let Ok(ds) = core::str::from_utf8(&state.date_str[..state.date_len]) {
860
+ self.add_line("Hardware RTC System Date:", 0x0038BDF8);
861
+ self.add_line(ds, 0x0034D399);
862
+ }
863
+ } else {
864
+ self.add_line("System Date: 23/11/2026", 0x0034D399);
865
+ }
866
+ } else if cmd == "time" {
867
+ if state.time_len > 0 {
868
+ if let Ok(ts) = core::str::from_utf8(&state.time_str[..state.time_len]) {
869
+ self.add_line("Hardware RTC System Time:", 0x0038BDF8);
870
+ self.add_line(ts, 0x002DD4BF);
871
+ }
872
+ } else {
873
+ self.add_line("System Time: 12:17 PM", 0x002DD4BF);
874
+ }
875
+ } else if cmd == "uname" || cmd == "uname -a" {
876
+ self.add_line("VyshOS 4.5.0-x86_64-uefi-microkernel #1 SMP JAIN-FET", 0x0038BDF8);
877
+ } else {
878
+ self.add_line("vysh-sh: command not recognized. Type 'help' for command list.", 0x00EF4444);
879
+ }
880
+
881
+ self.input_len = 0;
882
+ }
883
+ }
884
+
885
+ // ==========================================
886
+ // 5. DESKTOP APPLICATION ICONS
887
+ // ==========================================
888
+
889
+ fn draw_icon_terminal(disp: &Display, x: u32, y: u32) {
890
+ disp.round_card(x, y, 54, 54, 0x0018181B, 0x0022C55E);
891
+ disp.fill(x + 8, y + 8, 38, 34, 0x00000000);
892
+ disp.fill(x + 12, y + 14, 8, 2, 0x0022C55E);
893
+ disp.fill(x + 22, y + 20, 8, 2, 0x0022C55E);
894
+ draw_text(disp, x + 4, y + 59, "Terminal [T]", 0x0022C55E);
895
+ }
896
+
897
+ fn draw_icon_wifi(disp: &Display, x: u32, y: u32, is_online: bool) {
898
+ let col = if is_online { 0x000D9488 } else { 0x000369A1 };
899
+ let border = if is_online { 0x002DD4BF } else { 0x0038BDF8 };
900
+ disp.round_card(x, y, 54, 54, col, border);
901
+ disp.fill(x + 14, y + 12, 26, 3, 0x00FFFFFF);
902
+ disp.fill(x + 18, y + 19, 18, 3, 0x00FFFFFF);
903
+ disp.fill(x + 22, y + 26, 10, 3, 0x00FFFFFF);
904
+ disp.fill(x + 25, y + 33, 4, 4, 0x00FCD34D);
905
+ draw_text(disp, x + 6, y + 59, "Wi-Fi [W]", 0x00FFFFFF);
906
+ }
907
+
908
+ fn draw_icon_laptop(disp: &Display, x: u32, y: u32) {
909
+ disp.round_card(x, y, 54, 54, 0x000284C7, 0x0038BDF8);
910
+ disp.fill(x + 10, y + 10, 34, 22, 0x000F172A);
911
+ disp.fill(x + 12, y + 12, 30, 18, 0x0038BDF8);
912
+ disp.fill(x + 6, y + 34, 42, 6, 0x00CBD5E1);
913
+ draw_text(disp, x + 6, y + 59, "Laptop [L]", 0x00FFFFFF);
914
+ }
915
+
916
+ fn draw_icon_calc(disp: &Display, x: u32, y: u32) {
917
+ disp.round_card(x, y, 54, 54, 0x002563EB, 0x0060A5FA);
918
+ disp.fill(x + 10, y + 10, 34, 12, 0x000F172A);
919
+ disp.fill(x + 28, y + 13, 12, 6, 0x0034D399);
920
+ for r in 0..3 {
921
+ for c in 0..3 {
922
+ disp.fill(x + 10 + c * 9, y + 26 + r * 7, 7, 5, 0x003B82F6);
923
+ }
924
+ disp.fill(x + 37, y + 26 + r * 7, 7, 5, 0x00F59E0B);
925
+ }
926
+ draw_text(disp, x + 10, y + 59, "Calc [C]", 0x00FFFFFF);
927
+ }
928
+
929
+ fn draw_icon_settings(disp: &Display, x: u32, y: u32) {
930
+ disp.round_card(x, y, 54, 54, 0x00475569, 0x0094A3B8);
931
+ let cx = x + 27;
932
+ let cy = y + 25;
933
+ disp.fill(cx - 3, cy - 13, 6, 26, 0x00E2E8F0);
934
+ disp.fill(cx - 13, cy - 3, 26, 6, 0x00E2E8F0);
935
+ disp.fill(cx - 9, cy - 9, 18, 18, 0x00E2E8F0);
936
+ disp.fill(cx - 4, cy - 4, 8, 8, 0x00334155);
937
+ draw_text(disp, x + 2, y + 59, "Settings [S]", 0x00FFFFFF);
938
+ }
939
+
940
+ fn draw_icon_research(disp: &Display, x: u32, y: u32) {
941
+ disp.round_card(x, y, 54, 54, 0x007C3AED, 0x00A78BFA);
942
+ disp.fill(x + 12, y + 10, 30, 32, 0x00F8FAFC);
943
+ disp.fill(x + 16, y + 15, 22, 3, 0x007C3AED);
944
+ disp.fill(x + 16, y + 21, 16, 2, 0x0094A3B8);
945
+ disp.fill(x + 16, y + 26, 20, 2, 0x0094A3B8);
946
+ disp.fill(x + 16, y + 31, 12, 2, 0x0094A3B8);
947
+ draw_text(disp, x + 2, y + 59, "Research [R]", 0x00FFFFFF);
948
+ }
949
+
950
+ // ==========================================
951
+ // 6. MOUSE CURSOR
952
+ // ==========================================
953
+
954
+ fn draw_mouse(disp: &Display, x: u32, y: u32) {
955
+ let mask: [&str; 16] = [
956
+ "X ",
957
+ "XX ",
958
+ "X.X ",
959
+ "X..X ",
960
+ "X...X ",
961
+ "X....X ",
962
+ "X.....X ",
963
+ "X......X ",
964
+ "X.......X ",
965
+ "X.....XXXX ",
966
+ "X..X..X ",
967
+ "X.X X..X ",
968
+ "XX X..X ",
969
+ "X XX ",
970
+ " ",
971
+ " ",
972
+ ];
973
+
974
+ for r in 0..16 {
975
+ let line = mask[r].as_bytes();
976
+ for c in 0..16 {
977
+ let px = x + c as u32;
978
+ let py = y + r as u32;
979
+ if line[c] == b'X' {
980
+ disp.pixel(px, py, 0x00000000);
981
+ } else if line[c] == b'.' {
982
+ disp.pixel(px, py, 0x00FFFFFF);
983
+ }
984
+ }
985
+ }
986
+ }
987
+
988
+ pub struct MouseCursorBuffer {
989
+ pub saved_pixels: [u32; 256],
990
+ pub saved_x: u32,
991
+ pub saved_y: u32,
992
+ pub has_saved: bool,
993
+ }
994
+
995
+ impl MouseCursorBuffer {
996
+ pub const fn new() -> Self {
997
+ Self {
998
+ saved_pixels: [0; 256],
999
+ saved_x: 0,
1000
+ saved_y: 0,
1001
+ has_saved: false,
1002
+ }
1003
+ }
1004
+
1005
+ pub fn restore(&mut self, disp: &Display) {
1006
+ if self.has_saved {
1007
+ for r in 0..16 {
1008
+ for c in 0..16 {
1009
+ disp.pixel(self.saved_x + c as u32, self.saved_y + r as u32, self.saved_pixels[r * 16 + c]);
1010
+ }
1011
+ }
1012
+ self.has_saved = false;
1013
+ }
1014
+ }
1015
+
1016
+ pub fn draw(&mut self, disp: &Display, x: u32, y: u32) {
1017
+ self.restore(disp);
1018
+ for r in 0..16 {
1019
+ for c in 0..16 {
1020
+ self.saved_pixels[r * 16 + c] = disp.get_pixel(x + c as u32, y + r as u32);
1021
+ }
1022
+ }
1023
+ self.saved_x = x;
1024
+ self.saved_y = y;
1025
+ self.has_saved = true;
1026
+ draw_mouse(disp, x, y);
1027
+ }
1028
+ }
1029
+
1030
+ // ==========================================
1031
+ // 7. MASTER WORKSTATION RENDERING
1032
+ // ==========================================
1033
+
1034
+ #[derive(Clone, Copy)]
1035
+ pub struct OsState {
1036
+ pub screen_width: u32,
1037
+ pub screen_height: u32,
1038
+ pub active_app: u16, // 0 = Desktop, 1 = Laptop, 2 = Wi-Fi, 3 = Calc, 4 = Terminal, 5 = Settings, 6 = Research
1039
+ pub mouse_x: u32,
1040
+ pub mouse_y: u32,
1041
+ pub wifi_connected: bool,
1042
+ pub wifi_hotspot_id: u8,
1043
+ pub start_menu_open: bool,
1044
+ pub theme_mode: u8,
1045
+ pub time_str: [u8; 12],
1046
+ pub time_len: usize,
1047
+ pub date_str: [u8; 12],
1048
+ pub date_len: usize,
1049
+ pub last_minute: u8,
1050
+ }
1051
+
1052
+ fn click_at(state: &mut OsState, calc: &mut CalculatorState) -> bool {
1053
+ let x = state.mouse_x;
1054
+ let y = state.mouse_y;
1055
+
1056
+ let ww = 640;
1057
+ let wh = 450;
1058
+ let wx = if state.screen_width > ww { (state.screen_width - ww) / 2 } else { 40 };
1059
+ let wy = if state.screen_height > wh + 70 { (state.screen_height - 48 - wh) / 2 } else { 35 };
1060
+
1061
+ // Check Window Close Button [X]
1062
+ if state.active_app != 0 {
1063
+ if x >= wx + ww - 30 && x <= wx + ww && y >= wy && y <= wy + 32 {
1064
+ state.active_app = 0;
1065
+ return true;
1066
+ }
1067
+ }
1068
+
1069
+ // Check Calculator On-Screen Buttons if Calculator is open
1070
+ if state.active_app == '3' as u16 {
1071
+ // Keypad grid starts at (wx + 45, wy + 130)
1072
+ for r in 0..4 {
1073
+ for c in 0..4 {
1074
+ let bx = wx + 45 + c * 135;
1075
+ let by = wy + 130 + r * 62;
1076
+ if x >= bx && x <= bx + 120 && y >= by && y <= by + 52 {
1077
+ match (r, c) {
1078
+ (0, 0) => calc.input_digit(7),
1079
+ (0, 1) => calc.input_digit(8),
1080
+ (0, 2) => calc.input_digit(9),
1081
+ (0, 3) => calc.input_op(b'/'),
1082
+ (1, 0) => calc.input_digit(4),
1083
+ (1, 1) => calc.input_digit(5),
1084
+ (1, 2) => calc.input_digit(6),
1085
+ (1, 3) => calc.input_op(b'*'),
1086
+ (2, 0) => calc.input_digit(1),
1087
+ (2, 1) => calc.input_digit(2),
1088
+ (2, 2) => calc.input_digit(3),
1089
+ (2, 3) => calc.input_op(b'-'),
1090
+ (3, 0) => calc.input_digit(0),
1091
+ (3, 1) => calc.clear(),
1092
+ (3, 2) => calc.calculate(),
1093
+ (3, 3) => calc.input_op(b'+'),
1094
+ _ => {}
1095
+ }
1096
+ return true;
1097
+ }
1098
+ }
1099
+ }
1100
+ }
1101
+
1102
+ // Check Wi-Fi Center Cards if Wi-Fi Center is open
1103
+ if state.active_app == '2' as u16 {
1104
+ // Top status card (toggle connect/disconnect)
1105
+ if x >= wx + 32 && x <= wx + 32 + 538 && y >= wy + 60 && y <= wy + 60 + 38 {
1106
+ state.wifi_connected = !state.wifi_connected;
1107
+ return true;
1108
+ }
1109
+ // Card 1: Jain_Student
1110
+ if x >= wx + 32 && x <= wx + 32 + 538 && y >= wy + 128 && y <= wy + 128 + 38 {
1111
+ state.wifi_connected = true;
1112
+ state.wifi_hotspot_id = 1;
1113
+ return true;
1114
+ }
1115
+ // Card 2: Vyshuuu
1116
+ if x >= wx + 32 && x <= wx + 32 + 538 && y >= wy + 172 && y <= wy + 172 + 38 {
1117
+ state.wifi_connected = true;
1118
+ state.wifi_hotspot_id = 2;
1119
+ return true;
1120
+ }
1121
+ // Card 3: FET - Students
1122
+ if x >= wx + 32 && x <= wx + 32 + 538 && y >= wy + 216 && y <= wy + 216 + 38 {
1123
+ state.wifi_connected = true;
1124
+ state.wifi_hotspot_id = 3;
1125
+ return true;
1126
+ }
1127
+ // Card 4: Sammi's iPhone
1128
+ if x >= wx + 32 && x <= wx + 32 + 538 && y >= wy + 260 && y <= wy + 260 + 38 {
1129
+ state.wifi_connected = true;
1130
+ state.wifi_hotspot_id = 4;
1131
+ return true;
1132
+ }
1133
+ }
1134
+
1135
+ // Check Desktop Icons
1136
+ if x >= 26 && x <= 80 {
1137
+ if y >= 32 && y <= 86 {
1138
+ state.active_app = '4' as u16; // Terminal
1139
+ return true;
1140
+ } else if y >= 107 && y <= 161 {
1141
+ state.active_app = '2' as u16; // Wi-Fi
1142
+ return true;
1143
+ } else if y >= 182 && y <= 236 {
1144
+ state.active_app = '1' as u16; // Laptop
1145
+ return true;
1146
+ } else if y >= 257 && y <= 311 {
1147
+ state.active_app = '3' as u16; // Calc
1148
+ return true;
1149
+ } else if y >= 332 && y <= 386 {
1150
+ state.active_app = '5' as u16; // Settings
1151
+ return true;
1152
+ } else if y >= 407 && y <= 461 {
1153
+ state.active_app = '6' as u16; // Research
1154
+ return true;
1155
+ }
1156
+ }
1157
+
1158
+ // Check Start Button & Taskbar Pinned Icons (Bottom bar)
1159
+ let ty = state.screen_height - 48;
1160
+ if y >= ty {
1161
+ if x >= 12 && x <= 100 {
1162
+ state.start_menu_open = !state.start_menu_open;
1163
+ return true;
1164
+ }
1165
+ let icons_x = if state.screen_width > 500 { (state.screen_width - 200) / 2 } else { 330 };
1166
+ if x >= icons_x && x <= icons_x + 25 {
1167
+ state.active_app = '4' as u16; // Terminal
1168
+ state.start_menu_open = false;
1169
+ return true;
1170
+ } else if x >= icons_x + 30 && x <= icons_x + 55 {
1171
+ state.active_app = '2' as u16; // Wi-Fi
1172
+ state.start_menu_open = false;
1173
+ return true;
1174
+ } else if x >= icons_x + 60 && x <= icons_x + 85 {
1175
+ state.active_app = '1' as u16; // Laptop
1176
+ state.start_menu_open = false;
1177
+ return true;
1178
+ } else if x >= icons_x + 90 && x <= icons_x + 115 {
1179
+ state.active_app = '3' as u16; // Calc
1180
+ state.start_menu_open = false;
1181
+ return true;
1182
+ } else if x >= icons_x + 120 && x <= icons_x + 145 {
1183
+ state.active_app = '5' as u16; // Settings
1184
+ state.start_menu_open = false;
1185
+ return true;
1186
+ } else if x >= icons_x + 150 && x <= icons_x + 180 {
1187
+ state.active_app = '6' as u16; // Research
1188
+ state.start_menu_open = false;
1189
+ return true;
1190
+ }
1191
+ }
1192
+
1193
+ false
1194
+ }
1195
+
1196
+ fn render_os(disp: &Display, state: &OsState, term: &TerminalEngine, calc: &CalculatorState) {
1197
+ let base_color = if state.theme_mode == 0 { 0x000A192F } else { 0x00050814 };
1198
+ for y in 0..disp.height {
1199
+ let shade = (y * 28 / disp.height) as u32;
1200
+ let color = base_color + (shade << 8) + shade * 2;
1201
+ for x in 0..disp.width {
1202
+ disp.pixel(x, y, color);
1203
+ }
1204
+ }
1205
+
1206
+ // Top Helper Banner with ALL SHORTCUTS!
1207
+ disp.fill(0, 0, disp.width, 24, 0x000F172A);
1208
+ disp.fill(0, 23, disp.width, 1, 0x00334155);
1209
+ draw_text(disp, 12, 8, "Shortcuts: [L] Laptop | [W] Wi-Fi | [C] Calc | [T] Terminal | [S] Settings | [R] Research | [Esc] Close", 0x0038BDF8);
1210
+ draw_text(disp, disp.width - 480, 8, "Touchpad / Mousepad: Active", 0x0034D399);
1211
+ draw_text(disp, disp.width - 240, 8, "Student: Yeddula Vyshnavi", 0x0034D399);
1212
+
1213
+ // Left Icon Column
1214
+ draw_icon_terminal(disp, 26, 32);
1215
+ draw_icon_wifi(disp, 26, 107, state.wifi_connected);
1216
+ draw_icon_laptop(disp, 26, 182);
1217
+ draw_icon_calc(disp, 26, 257);
1218
+ draw_icon_settings(disp, 26, 332);
1219
+ draw_icon_research(disp, 26, 407);
1220
+
1221
+ // Windows
1222
+ if state.active_app != 0 {
1223
+ let ww = 640;
1224
+ let wh = 450;
1225
+ let wx = if disp.width > ww { (disp.width - ww) / 2 } else { 40 };
1226
+ let wy = if disp.height > wh + 70 { (disp.height - 48 - wh) / 2 } else { 35 };
1227
+
1228
+ disp.fill(wx + 6, wy + 6, ww, wh, 0x00050810);
1229
+ disp.fill(wx, wy, ww, wh, 0x000F172A);
1230
+
1231
+ disp.fill(wx, wy, ww, 34, 0x001E293B);
1232
+ disp.fill(wx, wy + 33, ww, 1, 0x00334155);
1233
+
1234
+ // Window Close Button [X]
1235
+ disp.fill(wx + ww - 65, wy + 10, 14, 14, 0x00334155);
1236
+ disp.fill(wx + ww - 45, wy + 10, 14, 14, 0x00334155);
1237
+ disp.fill(wx + ww - 26, wy + 7, 20, 20, 0x00EF4444);
1238
+ draw_text(disp, wx + ww - 21, wy + 11, "X", 0x00FFFFFF);
1239
+
1240
+ // ==========================================
1241
+ // APP 3: REAL WORKING INTERACTIVE CALCULATOR
1242
+ // ==========================================
1243
+ if state.active_app == '3' as u16 {
1244
+ draw_text(disp, wx + 16, wy + 13, "Calculator - Standard Scientific (Type numbers or click)", 0x0060A5FA);
1245
+ disp.fill(wx + 18, wy + 48, ww - 36, wh - 62, 0x001E293B);
1246
+
1247
+ // Digital LCD Display Screen
1248
+ disp.round_card(wx + 35, wy + 58, 570, 60, 0x000F172A, 0x0038BDF8);
1249
+
1250
+ // History expression above
1251
+ if calc.op != 0 {
1252
+ let mut exp_buf = [0u8; 24];
1253
+ let exp_len = format_i64(calc.stored_val, &mut exp_buf);
1254
+ if let Ok(s) = core::str::from_utf8(&exp_buf[..exp_len]) {
1255
+ draw_text(disp, wx + 50, wy + 66, s, 0x0094A3B8);
1256
+ let op_char = [calc.op];
1257
+ if let Ok(op_s) = core::str::from_utf8(&op_char) {
1258
+ draw_text(disp, wx + 50 + (exp_len as u32 * 6) + 4, wy + 66, op_s, 0x00FCD34D);
1259
+ }
1260
+ }
1261
+ } else if calc.has_result {
1262
+ draw_text(disp, wx + 50, wy + 66, "Ans =", 0x0034D399);
1263
+ }
1264
+
1265
+ // Main Large Digits Display
1266
+ if calc.is_error {
1267
+ draw_text(disp, wx + 50, wy + 84, "Error: Cannot divide by zero!", 0x00EF4444);
1268
+ } else {
1269
+ let mut num_buf = [0u8; 24];
1270
+ let num_len = format_i64(calc.display_val, &mut num_buf);
1271
+ if let Ok(s) = core::str::from_utf8(&num_buf[..num_len]) {
1272
+ let rx = wx + 580 - (num_len as u32 * 13);
1273
+ draw_text_large(disp, rx, wy + 82, s, 0x00FFFFFF);
1274
+ }
1275
+ }
1276
+
1277
+ // Realistic Keypad Grid (4 Rows x 4 Cols)
1278
+ let btn_labels: [[&str; 4]; 4] = [
1279
+ ["7", "8", "9", "/"],
1280
+ ["4", "5", "6", "*"],
1281
+ ["1", "2", "3", "-"],
1282
+ ["0", "C", "=", "+"],
1283
+ ];
1284
+
1285
+ for r in 0..4 {
1286
+ for c in 0..4 {
1287
+ let bx = wx + 45 + c * 135;
1288
+ let by = wy + 130 + r * 62;
1289
+ let col = if c == 3 {
1290
+ 0x00D97706 // Amber for operators
1291
+ } else if r == 3 && c == 2 {
1292
+ 0x00059669 // Emerald for Equals
1293
+ } else if r == 3 && c == 1 {
1294
+ 0x00DC2626 // Red for Clear [C]
1295
+ } else {
1296
+ 0x00334155 // Dark Slate for digits
1297
+ };
1298
+ disp.round_card(bx as u32, by as u32, 120, 52, col, 0x0064748B);
1299
+ draw_text_large(disp, bx as u32 + 54, by as u32 + 18, btn_labels[r as usize][c as usize], 0x00FFFFFF);
1300
+ }
1301
+ }
1302
+
1303
+ disp.fill(wx + 18, wy + wh - 36, ww - 36, 22, 0x000F172A);
1304
+ draw_text(disp, wx + 30, wy + wh - 28, "Keyboard: Type numbers, +, -, *, /, = (Enter), Backspace, C to Clear | Esc to close", 0x0094A3B8);
1305
+ }
1306
+
1307
+ // ==========================================
1308
+ // APP 4: TERMINAL (vysh-sh)
1309
+ // ==========================================
1310
+ else if state.active_app == '4' as u16 {
1311
+ draw_text(disp, wx + 16, wy + 13, "Terminal Shell - vysh-sh (Type 'exit' or press [Esc] to close)", 0x0022C55E);
1312
+ disp.fill(wx + 18, wy + 48, ww - 36, wh - 62, 0x00000000);
1313
+
1314
+ for i in 0..term.count {
1315
+ let ty_pos = wy + 58 + (i as u32 * 20);
1316
+ if let Ok(s) = core::str::from_utf8(&term.lines[i]) {
1317
+ draw_text(disp, wx + 28, ty_pos, s, term.colors[i]);
1318
+ }
1319
+ }
1320
+
1321
+ let prompt_y = wy + 58 + (term.count as u32 * 20);
1322
+ if prompt_y < wy + wh - 40 {
1323
+ draw_text(disp, wx + 28, prompt_y, "vysh-sh> ", 0x0034D399);
1324
+
1325
+ if let Ok(s) = core::str::from_utf8(&term.input_buf[..term.input_len]) {
1326
+ draw_text(disp, wx + 100, prompt_y, s, 0x00FFFFFF);
1327
+ }
1328
+
1329
+ let cursor_x = wx + 100 + (term.input_len as u32 * 8);
1330
+ disp.fill(cursor_x, prompt_y, 8, 16, 0x0022C55E);
1331
+ }
1332
+
1333
+ disp.fill(wx + 18, wy + wh - 38, ww - 36, 24, 0x0018181B);
1334
+ disp.fill(wx + 18, wy + wh - 38, ww - 36, 1, 0x00334155);
1335
+ draw_text(disp, wx + 28, wy + wh - 30, "Commands: wifi scan | wifi connect | ping 8.8.8.8 | help | exit [Press Esc to close]", 0x0094A3B8);
1336
+ }
1337
+
1338
+ // ==========================================
1339
+ // APP 2: WI-FI CENTER
1340
+ // ==========================================
1341
+ else if state.active_app == '2' as u16 {
1342
+ draw_text(disp, wx + 16, wy + 13, "Wi-Fi & Hotspot Center (Press [Esc] to close)", 0x00FFFFFF);
1343
+ disp.fill(wx + 18, wy + 48, ww - 36, wh - 62, 0x001E293B);
1344
+
1345
+ if state.wifi_connected {
1346
+ disp.round_card(wx + 32, wy + 60, 538, 38, 0x00134E4A, 0x002DD4BF);
1347
+ let conn_banner = match state.wifi_hotspot_id {
1348
+ 2 => "STATUS: CONNECTED TO 'VYSHUUU' (PERSONAL HOTSPOT)",
1349
+ 3 => "STATUS: CONNECTED TO 'FET - STUDENTS' (CAMPUS WI-FI)",
1350
+ 4 => "STATUS: CONNECTED TO 'SAMMI'S IPHONE' (HOTSPOT)",
1351
+ _ => "STATUS: CONNECTED TO 'JAIN_STUDENT' (CAMPUS WI-FI 6)",
1352
+ };
1353
+ draw_text(disp, wx + 44, wy + 72, conn_banner, 0x002DD4BF);
1354
+ } else {
1355
+ disp.round_card(wx + 32, wy + 60, 538, 38, 0x00450A0A, 0x00F87171);
1356
+ draw_text(disp, wx + 44, wy + 72, "STATUS: WI-FI DISCONNECTED - CLICK OR PRESS 1-4 TO CONNECT", 0x00FCA5A5);
1357
+ }
1358
+
1359
+ draw_text(disp, wx + 32, wy + 108, "Your Laptop Saved Networks & Hotspots (Press 1-4 to connect):", 0x0038BDF8);
1360
+
1361
+ // 1. Jain_Student
1362
+ let b1 = if state.wifi_connected && state.wifi_hotspot_id == 1 { 0x0010B981 } else { 0x00334155 };
1363
+ disp.round_card(wx + 32, wy + 128, 538, 38, 0x000F172A, b1);
1364
+ draw_text(disp, wx + 44, wy + 136, "[1] Jain_Student (JAIN University Campus Network)", 0x00FFFFFF);
1365
+ draw_text(disp, wx + 44, wy + 150, "Signal: 99% [||||] | Security: 802.1X Enterprise | Band: 5GHz", 0x0094A3B8);
1366
+ if state.wifi_connected && state.wifi_hotspot_id == 1 {
1367
+ draw_text(disp, wx + 450, wy + 142, "* CONNECTED *", 0x0034D399);
1368
+ }
1369
+
1370
+ // 2. Vyshuuu (sony5117)
1371
+ let b2 = if state.wifi_connected && state.wifi_hotspot_id == 2 { 0x0010B981 } else { 0x00334155 };
1372
+ disp.round_card(wx + 32, wy + 172, 538, 38, 0x000F172A, b2);
1373
+ draw_text(disp, wx + 44, wy + 180, "[2] Vyshuuu (Personal Hotspot - Password: sony5117)", 0x00FFFFFF);
1374
+ draw_text(disp, wx + 44, wy + 194, "Signal: 98% [||||] | Security: WPA3-Personal | Band: 5GHz", 0x0094A3B8);
1375
+ if state.wifi_connected && state.wifi_hotspot_id == 2 {
1376
+ draw_text(disp, wx + 450, wy + 186, "* CONNECTED *", 0x0034D399);
1377
+ }
1378
+
1379
+ // 3. FET - Students
1380
+ let b3 = if state.wifi_connected && state.wifi_hotspot_id == 3 { 0x0010B981 } else { 0x00334155 };
1381
+ disp.round_card(wx + 32, wy + 216, 538, 38, 0x000F172A, b3);
1382
+ draw_text(disp, wx + 44, wy + 224, "[3] FET - Students (JAIN FET Campus Network)", 0x00FFFFFF);
1383
+ draw_text(disp, wx + 44, wy + 238, "Signal: 95% [||||] | Security: 802.1X Enterprise | VLAN: 104", 0x0094A3B8);
1384
+ if state.wifi_connected && state.wifi_hotspot_id == 3 {
1385
+ draw_text(disp, wx + 450, wy + 230, "* CONNECTED *", 0x0034D399);
1386
+ }
1387
+
1388
+ // 4. Sammi's iPhone (Sammi@123)
1389
+ let b4 = if state.wifi_connected && state.wifi_hotspot_id == 4 { 0x0010B981 } else { 0x00334155 };
1390
+ disp.round_card(wx + 32, wy + 260, 538, 38, 0x000F172A, b4);
1391
+ draw_text(disp, wx + 44, wy + 268, "[4] Sammi's iPhone (Hotspot - Password: Sammi@123)", 0x00FFFFFF);
1392
+ draw_text(disp, wx + 44, wy + 282, "Signal: 90% [|||.] | Security: WPA2-PSK | Band: 2.4GHz", 0x0094A3B8);
1393
+ if state.wifi_connected && state.wifi_hotspot_id == 4 {
1394
+ draw_text(disp, wx + 450, wy + 274, "* CONNECTED *", 0x0034D399);
1395
+ }
1396
+
1397
+ // Bottom Diagnostics Card
1398
+ disp.round_card(wx + 32, wy + 306, 538, 92, 0x000F172A, 0x000D9488);
1399
+ if state.wifi_connected {
1400
+ let ip_str = if state.wifi_hotspot_id == 2 {
1401
+ "IPv4: 192.168.43.105 | Mask: 255.255.255.0 | Gateway: 192.168.43.1"
1402
+ } else if state.wifi_hotspot_id == 4 {
1403
+ "IPv4: 172.20.10.2 | Mask: 255.255.255.240 | Gateway: 172.20.10.1"
1404
+ } else {
1405
+ "IPv4: 172.20.253.58 | Mask: 255.255.192.0 | Gateway: 172.20.192.1"
1406
+ };
1407
+ draw_text(disp, wx + 44, wy + 318, ip_str, 0x002DD4BF);
1408
+ draw_text(disp, wx + 44, wy + 338, "Hardware Adapter: Intel(R) Wi-Fi 6 AX203 (B8:F7:75:A5:34:0D)", 0x00FFFFFF);
1409
+ draw_text(disp, wx + 44, wy + 358, "DNS: 172.20.192.1, 8.8.8.8 | Link Speed: 1.2 Gbps (Wi-Fi 6)", 0x0094A3B8);
1410
+ draw_text(disp, wx + 44, wy + 378, "Press 1-4 to switch network | Press [5] to disconnect | [Esc] close", 0x00FCD34D);
1411
+ } else {
1412
+ draw_text(disp, wx + 44, wy + 338, "Adapter: Intel(R) Wi-Fi 6 AX203 | Status: Radio Enabled, Disconnected", 0x00F87171);
1413
+ draw_text(disp, wx + 44, wy + 360, "Click any network above or press 1-4 to connect instantly", 0x00FFFFFF);
1414
+ }
1415
+ }
1416
+
1417
+ // ==========================================
1418
+ // APP 1: LAPTOP / THIS PC
1419
+ // ==========================================
1420
+ else if state.active_app == '1' as u16 {
1421
+ draw_text(disp, wx + 16, wy + 13, "This PC - Hardware Information (Press [Esc] to close)", 0x00FFFFFF);
1422
+ disp.fill(wx + 18, wy + 48, ww - 36, wh - 62, 0x001E293B);
1423
+
1424
+ draw_text(disp, wx + 35, wy + 64, "Storage Drives (Virtual SATA Controller):", 0x0038BDF8);
1425
+
1426
+ disp.round_card(wx + 35, wy + 88, 255, 54, 0x000F172A, 0x00334155);
1427
+ disp.fill(wx + 45, wy + 98, 30, 30, 0x000284C7);
1428
+ draw_text(disp, wx + 84, wy + 96, "Local Disk (C:) - UEFI FAT", 0x00FFFFFF);
1429
+ disp.fill(wx + 84, wy + 114, 180, 6, 0x00334155);
1430
+ disp.fill(wx + 84, wy + 114, 45, 6, 0x000284C7);
1431
+ draw_text(disp, wx + 84, wy + 125, "32 MB total (Kernel Active)", 0x0094A3B8);
1432
+
1433
+ disp.round_card(wx + 310, wy + 88, 255, 54, 0x000F172A, 0x00334155);
1434
+ disp.fill(wx + 320, wy + 98, 30, 30, 0x0010B981);
1435
+ draw_text(disp, wx + 358, wy + 96, "Data Disk (D:) - Ext4", 0x00FFFFFF);
1436
+ disp.fill(wx + 358, wy + 114, 180, 6, 0x00334155);
1437
+ disp.fill(wx + 358, wy + 114, 110, 6, 0x0010B981);
1438
+ draw_text(disp, wx + 358, wy + 125, "256 GB total (18 GB used)", 0x0094A3B8);
1439
+
1440
+ draw_text(disp, wx + 35, wy + 158, "System Files & Device Drivers:", 0x0038BDF8);
1441
+ draw_text(disp, wx + 35, wy + 180, "-> C:\\EFI\\BOOT\\BOOTX64.EFI (47,104 bytes) [UEFI Kernel]", 0x00FFFFFF);
1442
+ draw_text(disp, wx + 35, wy + 202, "-> C:\\OS\\Drivers\\wifi_80211.sys (48,120 bytes) [Hotspot Radio]", 0x0034D399);
1443
+ draw_text(disp, wx + 35, wy + 224, "-> C:\\OS\\Drivers\\e1000_nic.sys (32,768 bytes) [Gigabit LAN]", 0x00FFFFFF);
1444
+ draw_text(disp, wx + 35, wy + 246, "-> C:\\Users\\vyshn\\Research.pdf (1,248,500 B) [Scheduler]", 0x00A78BFA);
1445
+
1446
+ draw_text(disp, wx + 35, wy + 280, "Motherboard & Hardware Specifications:", 0x0038BDF8);
1447
+ draw_text(disp, wx + 35, wy + 304, "Processor : x86_64 Long Mode Dual-Core Virtual CPU @ 2.80GHz", 0x00FFFFFF);
1448
+ draw_text(disp, wx + 35, wy + 326, "Total RAM : 16.0 GB DDR4 High-Speed RAM (Direct Mapped)", 0x00FFFFFF);
1449
+ draw_text(disp, wx + 35, wy + 348, "Graphics : UEFI GOP Linear Framebuffer (1024x768x32bpp)", 0x00FFFFFF);
1450
+ draw_text(disp, wx + 35, wy + 370, "OS Platform : VyshOS Microkernel v4.5 [Safe Bare-Metal Rust]", 0x002DD4BF);
1451
+ }
1452
+
1453
+ // ==========================================
1454
+ // APP 5: SETTINGS
1455
+ // ==========================================
1456
+ else if state.active_app == '5' as u16 {
1457
+ draw_text(disp, wx + 16, wy + 13, "Settings & Device Specifications (Press [Esc] to close)", 0x00FFFFFF);
1458
+ disp.fill(wx + 18, wy + 48, ww - 36, wh - 62, 0x001E293B);
1459
+
1460
+ draw_text(disp, wx + 35, wy + 65, "Device & Student Credentials:", 0x0038BDF8);
1461
+ draw_text(disp, wx + 35, wy + 90, "Device Name : VYSHNAVI-LAPTOP-X86", 0x00FFFFFF);
1462
+ draw_text(disp, wx + 35, wy + 115, "Owner : Yeddula Vyshnavi", 0x0034D399);
1463
+ draw_text(disp, wx + 35, wy + 140, "USN : 25BTRCL294 (Section E)", 0x0034D399);
1464
+ draw_text(disp, wx + 35, wy + 165, "Institution : JAIN (Deemed-to-be University), FET Campus", 0x00FFFFFF);
1465
+ draw_text(disp, wx + 35, wy + 190, "Course : 25PCCS201 - Operating Systems", 0x0038BDF8);
1466
+
1467
+ draw_text(disp, wx + 35, wy + 225, "Network Adapter & Wi-Fi Stack:", 0x0038BDF8);
1468
+ draw_text(disp, wx + 35, wy + 250, "Hardware Adapter : Intel 82540EM / VirtualBox NAT Wireless", 0x00FFFFFF);
1469
+ draw_text(disp, wx + 35, wy + 275, "MAC Address : 08:00:27:B4:9C:2A", 0x00FFFFFF);
1470
+ draw_text(disp, wx + 35, wy + 300, "Active Hotspot : Vyshnavi_iPhone_5G (Signal 98%)", 0x002DD4BF);
1471
+ draw_text(disp, wx + 35, wy + 325, "Assigned IPv4 : 192.168.43.105 (Default GW 192.168.43.1)", 0x0034D399);
1472
+ }
1473
+
1474
+ // ==========================================
1475
+ // APP 6: RESEARCH SCHEDULER
1476
+ // ==========================================
1477
+ else if state.active_app == '6' as u16 {
1478
+ draw_text(disp, wx + 16, wy + 13, "OS Research Activity - Workload-Aware Scheduler", 0x00FFFFFF);
1479
+ disp.fill(wx + 18, wy + 48, ww - 36, wh - 62, 0x001E293B);
1480
+
1481
+ draw_text(disp, wx + 35, wy + 62, "Student: Yeddula Vyshnavi | USN: 25BTRCL294 | Sec: E", 0x0038BDF8);
1482
+ disp.fill(wx + 35, wy + 76, ww - 70, 1, 0x00334155);
1483
+
1484
+ draw_text(disp, wx + 35, wy + 88, "Workload Selection (PDF Algorithm Rules):", 0x0038BDF8);
1485
+ draw_text(disp, wx + 35, wy + 108, "1. Uniform 2. Interactive 3. Bimodal 4. Heavy-Tail 5. Batch", 0x00FFFFFF);
1486
+
1487
+ disp.round_card(wx + 35, wy + 130, 530, 42, 0x000F172A, 0x000284C7);
1488
+ draw_text(disp, wx + 48, wy + 142, "SELECTOR DECISION -> Shortest Job First (SJF)", 0x0038BDF8);
1489
+ draw_text(disp, wx + 48, wy + 156, "Rule 2 Activated: Batch (CV < 1.5) -> SJF Minimizes AWT", 0x0034D399);
1490
+
1491
+ let policies = ["FCFS", "SJF", "SRTF", "RR-10", "Workload-Aware"];
1492
+ let colors = [0x00EF4444, 0x003B82F6, 0x0010B981, 0x00F59E0B, 0x0038BDF8];
1493
+ let widths = [130, 60, 50, 110, 48];
1494
+
1495
+ for p in 0..5 {
1496
+ let py = wy + 210 + p as u32 * 26;
1497
+ draw_text(disp, wx + 35, py + 4, policies[p], 0x00FFFFFF);
1498
+ disp.fill(wx + 150, py, widths[p], 14, colors[p]);
1499
+ draw_text(disp, wx + 160 + widths[p], py + 4, "AWT Optimized", 0x0094A3B8);
1500
+ }
1501
+ }
1502
+ }
1503
+
1504
+ // Windows 11 Start Menu Popup (if open)
1505
+ if state.start_menu_open {
1506
+ let sx = 12;
1507
+ let sy = disp.height - 390;
1508
+ let sw = 330;
1509
+ let sh = 334;
1510
+
1511
+ disp.fill(sx + 4, sy + 4, sw, sh, 0x00050810);
1512
+ disp.fill(sx, sy, sw, sh, 0x000F172A);
1513
+ disp.fill(sx, sy, sw, 1, 0x0038BDF8);
1514
+
1515
+ disp.round_card(sx + 15, sy + 15, 300, 46, 0x001E293B, 0x00334155);
1516
+ disp.fill(sx + 25, sy + 23, 30, 30, 0x000284C7);
1517
+ draw_text(disp, sx + 32, sy + 32, "YV", 0x00FFFFFF);
1518
+ draw_text(disp, sx + 68, sy + 24, "Yeddula Vyshnavi", 0x00FFFFFF);
1519
+ draw_text(disp, sx + 68, sy + 38, "USN: 25BTRCL294 (Sec E)", 0x0034D399);
1520
+
1521
+ draw_text(disp, sx + 18, sy + 74, "Quick Launch Shortcuts:", 0x0038BDF8);
1522
+ draw_text(disp, sx + 18, sy + 100, "[L] Laptop & Hardware Specs", 0x00FFFFFF);
1523
+ draw_text(disp, sx + 18, sy + 126, "[W] Wi-Fi & Hotspot Center", 0x002DD4BF);
1524
+ draw_text(disp, sx + 18, sy + 152, "[C] Interactive Calculator", 0x00FFFFFF);
1525
+ draw_text(disp, sx + 18, sy + 178, "[T] Terminal (vysh-sh)", 0x0022C55E);
1526
+ draw_text(disp, sx + 18, sy + 204, "[S] Settings & System Info", 0x00FFFFFF);
1527
+ draw_text(disp, sx + 18, sy + 230, "[R] Research Scheduler (PDF)", 0x00A78BFA);
1528
+
1529
+ disp.fill(sx, sy + 288, sw, 1, 0x00334155);
1530
+ draw_text(disp, sx + 18, sy + 304, "[*] Universal Close: Press [Esc] or [F4]", 0x0094A3B8);
1531
+ }
1532
+
1533
+ // Taskbar (Bottom)
1534
+ let ty = disp.height - 48;
1535
+ disp.fill(0, ty, disp.width, 48, 0x000F172A);
1536
+ disp.fill(0, ty, disp.width, 1, 0x00334155);
1537
+
1538
+ // [ ❖ Start ] Button
1539
+ let sb_border = if state.start_menu_open { 0x0038BDF8 } else { 0x000284C7 };
1540
+ disp.round_card(12, ty + 6, 88, 36, 0x000284C7, sb_border);
1541
+ disp.fill(20, ty + 14, 5, 5, 0x00EF4444);
1542
+ disp.fill(27, ty + 14, 5, 5, 0x0010B981);
1543
+ disp.fill(20, ty + 21, 5, 5, 0x003B82F6);
1544
+ disp.fill(27, ty + 21, 5, 5, 0x00F59E0B);
1545
+ draw_text(disp, 38, ty + 18, "Start", 0x00FFFFFF);
1546
+
1547
+ // Search Box
1548
+ disp.round_card(110, ty + 6, 205, 36, 0x001E293B, 0x00475569);
1549
+ draw_text(disp, 122, ty + 18, "Search apps, wifi, files...", 0x0094A3B8);
1550
+
1551
+ // Centered Pinned Icons on Taskbar
1552
+ let icons_x = if disp.width > 500 { (disp.width - 200) / 2 } else { 330 };
1553
+ disp.fill(icons_x, ty + 14, 20, 20, 0x0018181B); // Terminal
1554
+ disp.fill(icons_x + 30, ty + 14, 20, 20, 0x000D9488); // Wi-Fi
1555
+ disp.fill(icons_x + 60, ty + 14, 20, 20, 0x000284C7); // Laptop
1556
+ disp.fill(icons_x + 90, ty + 14, 20, 20, 0x002563EB); // Calc
1557
+ disp.fill(icons_x + 120, ty + 14, 20, 20, 0x00475569); // Settings
1558
+ disp.fill(icons_x + 150, ty + 14, 20, 20, 0x007C3AED); // Research
1559
+
1560
+ if state.active_app == '4' as u16 {
1561
+ disp.fill(icons_x, ty + 38, 20, 2, 0x0022C55E);
1562
+ } else if state.active_app >= '1' as u16 && state.active_app <= '6' as u16 {
1563
+ let order = if state.active_app == '2' as u16 { 1 }
1564
+ else if state.active_app == '1' as u16 { 2 }
1565
+ else if state.active_app == '3' as u16 { 3 }
1566
+ else if state.active_app == '5' as u16 { 4 }
1567
+ else { 5 };
1568
+ disp.fill(icons_x + order * 30, ty + 38, 20, 2, 0x0038BDF8);
1569
+ }
1570
+
1571
+ // System Tray
1572
+ if state.wifi_connected {
1573
+ let name = match state.wifi_hotspot_id {
1574
+ 2 => "Wi-Fi: Vyshuuu",
1575
+ 3 => "Wi-Fi: FET-Student",
1576
+ 4 => "Wi-Fi: Sammi iPhone",
1577
+ _ => "Wi-Fi: Jain_Student",
1578
+ };
1579
+ disp.round_card(disp.width - 340, ty + 8, 150, 32, 0x00134E4A, 0x002DD4BF);
1580
+ draw_text(disp, disp.width - 334, ty + 18, name, 0x002DD4BF);
1581
+ } else {
1582
+ disp.round_card(disp.width - 340, ty + 8, 150, 32, 0x00334155, 0x0064748B);
1583
+ draw_text(disp, disp.width - 334, ty + 18, "Wi-Fi: Standby", 0x0094A3B8);
1584
+ }
1585
+
1586
+ disp.fill(disp.width - 180, ty + 26, 3, 6, 0x00FFFFFF);
1587
+ disp.fill(disp.width - 175, ty + 22, 3, 10, 0x00FFFFFF);
1588
+ disp.fill(disp.width - 170, ty + 18, 3, 14, 0x00FFFFFF);
1589
+
1590
+ disp.fill(disp.width - 150, ty + 20, 4, 10, 0x00FFFFFF);
1591
+ disp.fill(disp.width - 146, ty + 18, 6, 14, 0x00FFFFFF);
1592
+
1593
+ disp.fill(disp.width - 130, ty + 19, 20, 12, 0x00FFFFFF);
1594
+ disp.fill(disp.width - 110, ty + 22, 2, 6, 0x00FFFFFF);
1595
+ disp.fill(disp.width - 128, ty + 21, 14, 8, 0x0010B981);
1596
+
1597
+ // Live Hardware RTC Clock
1598
+ if state.time_len > 0 {
1599
+ if let Ok(ts) = core::str::from_utf8(&state.time_str[..state.time_len]) {
1600
+ draw_text(disp, disp.width - 96, ty + 15, ts, 0x00FFFFFF);
1601
+ }
1602
+ } else {
1603
+ draw_text(disp, disp.width - 96, ty + 15, "12:17 PM", 0x00FFFFFF);
1604
+ }
1605
+
1606
+ if state.date_len > 0 {
1607
+ if let Ok(ds) = core::str::from_utf8(&state.date_str[..state.date_len]) {
1608
+ draw_text(disp, disp.width - 96, ty + 27, ds, 0x0094A3B8);
1609
+ }
1610
+ } else {
1611
+ draw_text(disp, disp.width - 96, ty + 27, "23/11/2026", 0x0094A3B8);
1612
+ }
1613
+ }
1614
+
1615
+ fn update_time(rt: *mut RuntimeServices, state: &mut OsState) -> bool {
1616
+ if rt.is_null() {
1617
+ return false;
1618
+ }
1619
+ unsafe {
1620
+ let mut t = EfiTime::default();
1621
+ let status = ((*rt).get_time)(&mut t, 0);
1622
+ if status == 0 && t.year >= 2020 && t.year <= 2100 && t.month >= 1 && t.month <= 12 && t.day >= 1 && t.day <= 31 {
1623
+ if t.minute == state.last_minute && state.time_len > 0 {
1624
+ return false;
1625
+ }
1626
+ state.last_minute = t.minute;
1627
+
1628
+ // 12-hour format
1629
+ let is_pm = t.hour >= 12;
1630
+ let h12 = if t.hour == 0 {
1631
+ 12
1632
+ } else if t.hour > 12 {
1633
+ t.hour - 12
1634
+ } else {
1635
+ t.hour
1636
+ };
1637
+
1638
+ let mut tb = [b' '; 12];
1639
+ let mut idx = 0;
1640
+ if h12 < 10 {
1641
+ tb[idx] = b'0' + h12 as u8;
1642
+ idx += 1;
1643
+ } else {
1644
+ tb[idx] = b'0' + (h12 / 10) as u8;
1645
+ idx += 1;
1646
+ tb[idx] = b'0' + (h12 % 10) as u8;
1647
+ idx += 1;
1648
+ }
1649
+ tb[idx] = b':';
1650
+ idx += 1;
1651
+ tb[idx] = b'0' + (t.minute / 10) as u8;
1652
+ idx += 1;
1653
+ tb[idx] = b'0' + (t.minute % 10) as u8;
1654
+ idx += 1;
1655
+ tb[idx] = b' ';
1656
+ idx += 1;
1657
+ if is_pm {
1658
+ tb[idx] = b'P';
1659
+ idx += 1;
1660
+ tb[idx] = b'M';
1661
+ idx += 1;
1662
+ } else {
1663
+ tb[idx] = b'A';
1664
+ idx += 1;
1665
+ tb[idx] = b'M';
1666
+ idx += 1;
1667
+ }
1668
+ state.time_str = tb;
1669
+ state.time_len = idx;
1670
+
1671
+ // Format Date: DD/MM/YYYY (e.g. 23/11/2026)
1672
+ let mut db = [b' '; 12];
1673
+ let mut didx = 0;
1674
+ db[didx] = b'0' + (t.day / 10) as u8;
1675
+ didx += 1;
1676
+ db[didx] = b'0' + (t.day % 10) as u8;
1677
+ didx += 1;
1678
+ db[didx] = b'/';
1679
+ didx += 1;
1680
+ db[didx] = b'0' + (t.month / 10) as u8;
1681
+ didx += 1;
1682
+ db[didx] = b'0' + (t.month % 10) as u8;
1683
+ didx += 1;
1684
+ db[didx] = b'/';
1685
+ didx += 1;
1686
+
1687
+ let yr = t.year;
1688
+ db[didx] = b'0' + ((yr / 1000) % 10) as u8;
1689
+ didx += 1;
1690
+ db[didx] = b'0' + ((yr / 100) % 10) as u8;
1691
+ didx += 1;
1692
+ db[didx] = b'0' + ((yr / 10) % 10) as u8;
1693
+ didx += 1;
1694
+ db[didx] = b'0' + (yr % 10) as u8;
1695
+ didx += 1;
1696
+
1697
+ state.date_str = db;
1698
+ state.date_len = didx;
1699
+
1700
+ return true;
1701
+ }
1702
+
1703
+ // Fallback if hardware RTC not returned
1704
+ if state.time_len == 0 {
1705
+ let t_init = b"12:17 PM";
1706
+ state.time_str[..t_init.len()].copy_from_slice(t_init);
1707
+ state.time_len = t_init.len();
1708
+
1709
+ let d_init = b"23/11/2026";
1710
+ state.date_str[..d_init.len()].copy_from_slice(d_init);
1711
+ state.date_len = d_init.len();
1712
+ return true;
1713
+ }
1714
+
1715
+ false
1716
+ }
1717
+ }
1718
+
1719
+ fn select_best_resolution(gop: *mut GraphicsOutputProtocol) {
1720
+ unsafe {
1721
+ if gop.is_null() || (*gop).mode.is_null() {
1722
+ return;
1723
+ }
1724
+ let mode_ptr = (*gop).mode;
1725
+ let max_modes = (*mode_ptr).max_mode;
1726
+ let mut best_mode = (*mode_ptr).mode;
1727
+ let mut best_pixels: u64 = 0;
1728
+
1729
+ if !(*mode_ptr).info.is_null() {
1730
+ best_pixels = ((*(*mode_ptr).info).horizontal_resolution as u64)
1731
+ * ((*(*mode_ptr).info).vertical_resolution as u64);
1732
+ }
1733
+
1734
+ for m in 0..max_modes {
1735
+ let mut info_size: usize = 0;
1736
+ let mut info_ptr: *mut GopModeInfo = core::ptr::null_mut();
1737
+ let status = ((*gop).query_mode)(gop, m, &mut info_size, &mut info_ptr);
1738
+ if status == 0 && !info_ptr.is_null() {
1739
+ let w = (*info_ptr).horizontal_resolution;
1740
+ let h = (*info_ptr).vertical_resolution;
1741
+ let px = (w as u64) * (h as u64);
1742
+ // Prefer 1920x1080 Full HD, or highest resolution available
1743
+ if (w == 1920 && h == 1080) || px > best_pixels {
1744
+ best_pixels = px;
1745
+ best_mode = m;
1746
+ if w == 1920 && h == 1080 {
1747
+ break;
1748
+ }
1749
+ }
1750
+ }
1751
+ }
1752
+
1753
+ if best_mode != (*mode_ptr).mode {
1754
+ let _ = ((*gop).set_mode)(gop, best_mode);
1755
+ }
1756
+ }
1757
+ }
1758
+
1759
+ // ==========================================
1760
+ // 8. MAIN KERNEL ENTRY POINT
1761
+ // ==========================================
1762
+
1763
+ #[unsafe(no_mangle)]
1764
+ pub extern "efiapi" fn efi_main(
1765
+ _image_handle: usize,
1766
+ system_table: *mut SystemTable,
1767
+ ) -> usize {
1768
+ unsafe {
1769
+ let boot_services = (*system_table).boot_services;
1770
+ let con_in = (*system_table).con_in;
1771
+ let rt = (*system_table).runtime_services;
1772
+
1773
+ let mut gop: *mut GraphicsOutputProtocol = core::ptr::null_mut();
1774
+ let status = ((*boot_services).locate_protocol)(
1775
+ &GOP_GUID,
1776
+ 0,
1777
+ &mut gop as *mut _ as *mut usize,
1778
+ );
1779
+
1780
+ let mut abs_pointer: *mut EfiAbsolutePointerProtocol = core::ptr::null_mut();
1781
+ let _abs_status = ((*boot_services).locate_protocol)(
1782
+ &ABSOLUTE_POINTER_GUID,
1783
+ 0,
1784
+ &mut abs_pointer as *mut _ as *mut usize,
1785
+ );
1786
+
1787
+ let mut pointer: *mut EfiSimplePointerProtocol = core::ptr::null_mut();
1788
+ let _rel_status = ((*boot_services).locate_protocol)(
1789
+ &POINTER_GUID,
1790
+ 0,
1791
+ &mut pointer as *mut _ as *mut usize,
1792
+ );
1793
+
1794
+ if !abs_pointer.is_null() {
1795
+ let _ = ((*abs_pointer).reset)(abs_pointer, false);
1796
+ }
1797
+ if !pointer.is_null() {
1798
+ let _ = ((*pointer).reset)(pointer, false);
1799
+ }
1800
+
1801
+ if status == 0 && !gop.is_null() {
1802
+ select_best_resolution(gop);
1803
+
1804
+ let mode = (*gop).mode;
1805
+ let info = (*mode).info;
1806
+
1807
+ let display = Display {
1808
+ fb: (*mode).frame_buffer_base as *mut u32,
1809
+ width: (*info).horizontal_resolution,
1810
+ height: (*info).vertical_resolution,
1811
+ stride: (*info).pixels_per_scan_line,
1812
+ };
1813
+
1814
+ let mut state = OsState {
1815
+ screen_width: display.width,
1816
+ screen_height: display.height,
1817
+ active_app: 0, // Clean Desktop on boot with shortcut banner!
1818
+ mouse_x: display.width / 2,
1819
+ mouse_y: display.height / 2,
1820
+ wifi_connected: true, // Automatically connected to hotspot!
1821
+ wifi_hotspot_id: 1, // Vyshnavi iPhone Hotspot!
1822
+ start_menu_open: false,
1823
+ theme_mode: 0,
1824
+ time_str: [b' '; 12],
1825
+ time_len: 0,
1826
+ date_str: [b' '; 12],
1827
+ date_len: 0,
1828
+ last_minute: 255,
1829
+ };
1830
+
1831
+ let mut term = TerminalEngine::new();
1832
+ let mut calc = CalculatorState::new();
1833
+
1834
+ let mut hw_mouse = Ps2Mouse::new();
1835
+ hw_mouse.init();
1836
+ let mut was_mouse_pressed = false;
1837
+
1838
+ update_time(rt, &mut state);
1839
+ render_os(&display, &state, &term, &calc);
1840
+
1841
+ let mut mouse_buf = MouseCursorBuffer::new();
1842
+ mouse_buf.draw(&display, state.mouse_x, state.mouse_y);
1843
+
1844
+ let mut p_state = EfiSimplePointerState {
1845
+ relative_movement_x: 0,
1846
+ relative_movement_y: 0,
1847
+ relative_movement_z: 0,
1848
+ left_button: false,
1849
+ right_button: false,
1850
+ };
1851
+ let mut was_rel_pressed = false;
1852
+
1853
+ let mut abs_state = EfiAbsolutePointerState {
1854
+ current_x: 0,
1855
+ current_y: 0,
1856
+ current_z: 0,
1857
+ active_buttons: 0,
1858
+ };
1859
+ let mut was_abs_pressed = false;
1860
+
1861
+ let mut key = InputKey { scan_code: 0, unicode_char: 0 };
1862
+ let mut tick_counter: u32 = 0;
1863
+
1864
+ loop {
1865
+ let mut screen_needs_render = false;
1866
+ let mut moved = false;
1867
+
1868
+ tick_counter = tick_counter.wrapping_add(1);
1869
+ if tick_counter % 50_000 == 0 {
1870
+ if update_time(rt, &mut state) {
1871
+ screen_needs_render = true;
1872
+ }
1873
+ }
1874
+
1875
+ // 1. Check Hardware PS/2 Touchpad / Mouse (Direct 8042 Port 0x60/0x64)
1876
+ let mut mouse_dx = 0;
1877
+ let mut mouse_dy = 0;
1878
+ let mut mouse_clicked = false;
1879
+ let mut any_mouse = false;
1880
+
1881
+ while let Some((dx, dy, left_click)) = hw_mouse.poll() {
1882
+ mouse_dx += dx;
1883
+ mouse_dy += dy;
1884
+ if left_click {
1885
+ mouse_clicked = true;
1886
+ }
1887
+ any_mouse = true;
1888
+ }
1889
+
1890
+ if any_mouse {
1891
+ if mouse_dx != 0 || mouse_dy != 0 {
1892
+ let nx = state.mouse_x as i32 + (mouse_dx * 2);
1893
+ state.mouse_x = if nx < 5 { 5 } else if nx > (display.width - 20) as i32 { display.width - 20 } else { nx as u32 };
1894
+
1895
+ let ny = state.mouse_y as i32 + (mouse_dy * 2);
1896
+ state.mouse_y = if ny < 5 { 5 } else if ny > (display.height - 25) as i32 { display.height - 25 } else { ny as u32 };
1897
+
1898
+ moved = true;
1899
+ }
1900
+
1901
+ if mouse_clicked && !was_mouse_pressed {
1902
+ if click_at(&mut state, &mut calc) {
1903
+ screen_needs_render = true;
1904
+ }
1905
+ }
1906
+ was_mouse_pressed = mouse_clicked;
1907
+ }
1908
+
1909
+ // 2. Check Touchpad / Laptop Trackpad (UEFI Absolute Pointer)
1910
+ if !abs_pointer.is_null() {
1911
+ let s = ((*abs_pointer).get_state)(abs_pointer, &mut abs_state);
1912
+ if s == 0 {
1913
+ let mode_ptr = (*abs_pointer).mode;
1914
+ let (min_x, max_x, min_y, max_y) = if !mode_ptr.is_null() && (*mode_ptr).absolute_max_x > (*mode_ptr).absolute_min_x {
1915
+ ((*mode_ptr).absolute_min_x, (*mode_ptr).absolute_max_x, (*mode_ptr).absolute_min_y, (*mode_ptr).absolute_max_y)
1916
+ } else {
1917
+ (0, 32767, 0, 32767)
1918
+ };
1919
+
1920
+ let span_x = if max_x > min_x { max_x - min_x } else { 1 };
1921
+ let span_y = if max_y > min_y { max_y - min_y } else { 1 };
1922
+
1923
+ let clamped_x = if abs_state.current_x < min_x {
1924
+ min_x
1925
+ } else if abs_state.current_x > max_x {
1926
+ max_x
1927
+ } else {
1928
+ abs_state.current_x
1929
+ };
1930
+
1931
+ let clamped_y = if abs_state.current_y < min_y {
1932
+ min_y
1933
+ } else if abs_state.current_y > max_y {
1934
+ max_y
1935
+ } else {
1936
+ abs_state.current_y
1937
+ };
1938
+
1939
+ let new_x = ((clamped_x - min_x) * (display.width as u64) / span_x) as u32;
1940
+ let new_y = ((clamped_y - min_y) * (display.height as u64) / span_y) as u32;
1941
+
1942
+ let final_x = if new_x >= display.width - 20 { display.width - 20 } else { new_x };
1943
+ let final_y = if new_y >= display.height - 25 { display.height - 25 } else { new_y };
1944
+
1945
+ if final_x != state.mouse_x || final_y != state.mouse_y {
1946
+ state.mouse_x = final_x;
1947
+ state.mouse_y = final_y;
1948
+ moved = true;
1949
+ }
1950
+
1951
+ let is_pressed = (abs_state.active_buttons & 1) != 0;
1952
+ if is_pressed && !was_abs_pressed {
1953
+ if click_at(&mut state, &mut calc) {
1954
+ screen_needs_render = true;
1955
+ }
1956
+ }
1957
+ was_abs_pressed = is_pressed;
1958
+ }
1959
+ }
1960
+
1961
+ // 3. Check Relative Mouse Pointer (Physical Mouse / PS2)
1962
+ if !pointer.is_null() {
1963
+ let ps = ((*pointer).get_state)(pointer, &mut p_state);
1964
+ if ps == 0 {
1965
+ let dx = p_state.relative_movement_x / 30;
1966
+ let dy = p_state.relative_movement_y / 30;
1967
+ if dx != 0 {
1968
+ let nx = state.mouse_x as i32 + dx;
1969
+ state.mouse_x = if nx < 5 { 5 } else if nx > (display.width - 20) as i32 { display.width - 20 } else { nx as u32 };
1970
+ moved = true;
1971
+ }
1972
+ if dy != 0 {
1973
+ let ny = state.mouse_y as i32 + dy;
1974
+ state.mouse_y = if ny < 5 { 5 } else if ny > (display.height - 25) as i32 { display.height - 25 } else { ny as u32 };
1975
+ moved = true;
1976
+ }
1977
+ if p_state.left_button && !was_rel_pressed {
1978
+ if click_at(&mut state, &mut calc) {
1979
+ screen_needs_render = true;
1980
+ }
1981
+ }
1982
+ was_rel_pressed = p_state.left_button;
1983
+ }
1984
+ }
1985
+
1986
+ // 4. Check Keyboard Strokes
1987
+ let s = ((*con_in).read_key_stroke)(con_in, &mut key);
1988
+ if s == 0 {
1989
+ // 1. UNIVERSAL CLOSE SHORTCUT: ESCAPE (Esc) OR F4 CLOSES ANY APP INSTANTLY!
1990
+ if key.unicode_char == 27 || key.scan_code == 23 || key.scan_code == 14 {
1991
+ if state.active_app != 0 {
1992
+ state.active_app = 0;
1993
+ state.start_menu_open = false;
1994
+ screen_needs_render = true;
1995
+ } else if state.start_menu_open {
1996
+ state.start_menu_open = false;
1997
+ screen_needs_render = true;
1998
+ }
1999
+ }
2000
+ // 2. When TERMINAL is ACTIVE, handle full shell keyboard typing!
2001
+ else if state.active_app == '4' as u16 {
2002
+ if key.unicode_char == 13 || key.unicode_char == b'\n' as u16 {
2003
+ term.execute(&mut state);
2004
+ screen_needs_render = true;
2005
+ } else if key.unicode_char == 8 {
2006
+ if term.input_len > 0 {
2007
+ term.input_len -= 1;
2008
+ screen_needs_render = true;
2009
+ }
2010
+ } else if key.unicode_char >= 32 && key.unicode_char <= 126 {
2011
+ if term.input_len < 40 {
2012
+ term.input_buf[term.input_len] = key.unicode_char as u8;
2013
+ term.input_len += 1;
2014
+ screen_needs_render = true;
2015
+ }
2016
+ }
2017
+ }
2018
+ // 3. ARROW KEYS ALWAYS MOVE THE MOUSE CURSOR AS RELIABLE BACKUP!
2019
+ else if key.scan_code == 1 { // UP
2020
+ if state.mouse_y > 15 { state.mouse_y -= 25; }
2021
+ moved = true;
2022
+ } else if key.scan_code == 2 { // DOWN
2023
+ if state.mouse_y < display.height - 35 { state.mouse_y += 25; }
2024
+ moved = true;
2025
+ } else if key.scan_code == 4 { // LEFT
2026
+ if state.mouse_x > 15 { state.mouse_x -= 25; }
2027
+ moved = true;
2028
+ } else if key.scan_code == 3 { // RIGHT
2029
+ if state.mouse_x < display.width - 25 { state.mouse_x += 25; }
2030
+ moved = true;
2031
+ }
2032
+ // 4. SPACE KEY CLICKS UNDER CURSOR (When outside Terminal)!
2033
+ else if key.unicode_char == b' ' as u16 {
2034
+ if click_at(&mut state, &mut calc) {
2035
+ screen_needs_render = true;
2036
+ }
2037
+ }
2038
+ // 5. When CALCULATOR is ACTIVE, handle full real calculation math keys!
2039
+ else if state.active_app == '3' as u16 {
2040
+ let ch = key.unicode_char as u8;
2041
+ if ch >= b'0' && ch <= b'9' {
2042
+ calc.input_digit(ch - b'0');
2043
+ screen_needs_render = true;
2044
+ } else if ch == b'+' || ch == b'-' || ch == b'*' || ch == b'/' {
2045
+ calc.input_op(ch);
2046
+ screen_needs_render = true;
2047
+ } else if ch == b'=' || key.unicode_char == 13 || key.unicode_char == b'\n' as u16 {
2048
+ calc.calculate();
2049
+ screen_needs_render = true;
2050
+ } else if key.unicode_char == 8 {
2051
+ calc.backspace();
2052
+ screen_needs_render = true;
2053
+ } else if ch == b'c' || ch == b'C' {
2054
+ calc.clear();
2055
+ screen_needs_render = true;
2056
+ }
2057
+ }
2058
+ // 6. When WI-FI CENTER is ACTIVE, 1-4 selects network, 5 disconnects!
2059
+ else if state.active_app == '2' as u16 && (key.unicode_char >= '1' as u16 && key.unicode_char <= '5' as u16) {
2060
+ if key.unicode_char == '1' as u16 {
2061
+ state.wifi_connected = true;
2062
+ state.wifi_hotspot_id = 1;
2063
+ } else if key.unicode_char == '2' as u16 {
2064
+ state.wifi_connected = true;
2065
+ state.wifi_hotspot_id = 2;
2066
+ } else if key.unicode_char == '3' as u16 {
2067
+ state.wifi_connected = true;
2068
+ state.wifi_hotspot_id = 3;
2069
+ } else if key.unicode_char == '4' as u16 {
2070
+ state.wifi_connected = true;
2071
+ state.wifi_hotspot_id = 4;
2072
+ } else if key.unicode_char == '5' as u16 {
2073
+ state.wifi_connected = false;
2074
+ state.wifi_hotspot_id = 0;
2075
+ }
2076
+ screen_needs_render = true;
2077
+ }
2078
+ // 7. FAST APPLICATION SHORTCUTS (L, W, C, T, S, R, and 1-6):
2079
+ else {
2080
+ // [L] -> Laptop / This PC
2081
+ if key.unicode_char == 'l' as u16 || key.unicode_char == 'L' as u16
2082
+ || key.unicode_char == '1' as u16 {
2083
+ state.active_app = '1' as u16;
2084
+ state.start_menu_open = false;
2085
+ screen_needs_render = true;
2086
+ }
2087
+ // [W] -> Wi-Fi & Hotspots
2088
+ else if key.unicode_char == 'w' as u16 || key.unicode_char == 'W' as u16
2089
+ || key.unicode_char == '2' as u16 {
2090
+ state.active_app = '2' as u16;
2091
+ state.start_menu_open = false;
2092
+ screen_needs_render = true;
2093
+ }
2094
+ // [C] -> Calculator
2095
+ else if key.unicode_char == 'c' as u16 || key.unicode_char == 'C' as u16 {
2096
+ state.active_app = '3' as u16;
2097
+ state.start_menu_open = false;
2098
+ screen_needs_render = true;
2099
+ }
2100
+ // [T] -> Terminal
2101
+ else if key.unicode_char == 't' as u16 || key.unicode_char == 'T' as u16
2102
+ || key.unicode_char == '4' as u16 {
2103
+ state.active_app = '4' as u16;
2104
+ state.start_menu_open = false;
2105
+ screen_needs_render = true;
2106
+ }
2107
+ // [S] -> Settings
2108
+ else if key.unicode_char == 's' as u16 || key.unicode_char == 'S' as u16
2109
+ || key.unicode_char == '5' as u16 {
2110
+ state.active_app = '5' as u16;
2111
+ state.start_menu_open = false;
2112
+ screen_needs_render = true;
2113
+ }
2114
+ // [R] -> Research Paper
2115
+ else if key.unicode_char == 'r' as u16 || key.unicode_char == 'R' as u16
2116
+ || key.unicode_char == '6' as u16 {
2117
+ state.active_app = '6' as u16;
2118
+ state.start_menu_open = false;
2119
+ screen_needs_render = true;
2120
+ }
2121
+ // [Tab] -> Toggle Start Menu
2122
+ else if key.unicode_char == 9 {
2123
+ state.start_menu_open = !state.start_menu_open;
2124
+ screen_needs_render = true;
2125
+ }
2126
+ }
2127
+ }
2128
+
2129
+ // 5. Blit screen or dirty-rect cursor update
2130
+ if screen_needs_render {
2131
+ render_os(&display, &state, &term, &calc);
2132
+ mouse_buf.has_saved = false;
2133
+ mouse_buf.draw(&display, state.mouse_x, state.mouse_y);
2134
+ } else if moved {
2135
+ mouse_buf.draw(&display, state.mouse_x, state.mouse_y);
2136
+ }
2137
+ }
2138
+ }
2139
+ }
2140
+
2141
+ loop {}
2142
+ }