Meta: Add files to repository

This commit is contained in:
Alec Murphy
2025-02-16 15:21:19 -05:00
parent 6d27d43268
commit 52cb92f587
120 changed files with 71820 additions and 0 deletions

29
src/net/os/ioport.jakt Normal file
View File

@@ -0,0 +1,29 @@
import extern c "ioport.h" {
extern fn ioport_read_u8(address: u16) -> u8
extern fn ioport_read_u16(address: u16) -> u16
extern fn ioport_read_u32(address: u16) -> u32
extern fn ioport_write_u8(address: u16, value: u8)
extern fn ioport_write_u16(address: u16, value: u16)
extern fn ioport_write_u32(address: u16, value: u32)
}
struct IOPort {
fn read_u8(anon address: u16) throws -> u8 {
return ioport_read_u8(address)
}
fn read_u16(anon address: u16) throws -> u16 {
return ioport_read_u16(address)
}
fn read_u32(anon address: u16) throws -> u32 {
return ioport_read_u32(address)
}
fn write_u8(address: u16, value: u8) {
return ioport_write_u8(address, value)
}
fn write_u16(address: u16, value: u16) {
return ioport_write_u16(address, value)
}
fn write_u32(address: u16, value: u32) {
return ioport_write_u32(address, value)
}
}

154
src/net/os/os.jakt Normal file
View File

@@ -0,0 +1,154 @@
import extern c "os.h" {
extern fn os_blink(frequency: raw c_char) -> bool
extern fn os_call(function_name: u64, arg: u64) -> u64
extern fn os_device_calloc(size: u32) -> u32
extern fn os_exit()
extern fn os_file_picker(path: raw c_char, glob: raw c_char)
extern fn os_files_list(path: raw c_char)
extern fn os_is_vm() -> bool
extern fn os_path_exists(anon path: raw c_char) -> bool
extern fn os_pc_speaker(frequency: raw c_char)
extern fn os_random() -> u64
extern fn os_screenshot()
extern fn os_to_uppercase(anon input_string: raw c_char) -> raw c_char
}
struct OS {
fn blink(frequency: f64 = 2.5) throws -> bool {
let frequency_as_string = format("{}", frequency)
return os_blink(frequency: frequency_as_string.c_string())
}
fn call(anon function_name: String, anon arg: String) throws -> u64 {
mut res: u64 = 0
unsafe {
cpp {
"
res = os_call((u64)function_name.characters(), (u64)arg.characters());
"
}
}
return res
}
fn device_calloc(anon size: u32) throws -> u32 {
return os_device_calloc(size)
}
fn device_copy_buffer(anon buffer: [u8]) -> u32 {
mut address: u32 = 0
mut size = buffer.size()
unsafe {
cpp {
"u8 *data = (u8*)os_device_calloc(size);
for (int i = 0; i < size; i++)
data[i] = buffer[i];
address = (uintptr_t)data;"
}
}
return address
}
fn exit() {
os_exit()
}
fn file_picker(path: String, glob: String) throws -> String {
mut s = StringBuilder::create()
unsafe {
cpp {
"char const *chars = os_file_picker(path.characters(), glob.characters());
s.append_c_string(chars);
delete(chars);"
}
}
return s.to_string()
}
fn files_list(path: String) throws -> [String] {
mut s = StringBuilder::create()
unsafe {
cpp {
"char const *chars = os_files_list(path.characters());
if (chars) {
s.append_c_string(chars);
delete(chars);
}"
}
}
return s.to_string().split(c'|')
}
fn path_exists(anon path: String) -> bool {
return os_path_exists(path.c_string())
}
fn is_vm() -> bool {
return os_is_vm()
}
fn pc_speaker(frequency: f64) throws {
let frequency_as_string = format("{}", frequency)
os_pc_speaker(frequency: frequency_as_string.c_string())
}
fn put_char(ch: u8) {
unsafe {
cpp {
"putchar(ch);"
}
}
}
fn random() -> u64 {
return os_random()
}
fn read_entire_file(anon filename: String) throws -> [u8] {
mut size = 0
mut buffer: [u8] = []
unsafe {
cpp {
"u8 *data = os_read_entire_file(filename.characters(), &size);
for (int i = 0; i < size; i++)
buffer.push(data[i]);
free(data);"
}
}
return buffer
}
fn read_device_memory(address: u32, size: i64) throws -> [u8] {
mut buffer: [u8] = [];
unsafe {
cpp {
"u8 *device_memory = (u8*)address;
for (int i = 0; i < size; i++)
buffer.push(device_memory[i]);"
}
}
return buffer
}
fn read_u16_from_device_memory(anon address: u32) throws -> u16 {
mut value: u16 = 0
unsafe {
cpp {
"value = *(u16*)address;"
}
}
return value
}
fn screenshot() {
os_screenshot()
}
fn to_uppercase(anon input_string: String) throws -> String {
mut s = StringBuilder::create()
unsafe {
cpp {
"char const *chars = os_to_uppercase(input_string.characters());
s.append_c_string(chars);
delete(chars);"
}
}
return s.to_string()
}
fn write_entire_file(filename: String, buffer: [u8]) {
mut size = buffer.size()
unsafe {
cpp {
"unsigned char *data = (unsigned char *)malloc(size);
for (int i = 0; i < size; i++)
data[i] = buffer[i];
os_write_entire_file(filename.characters(), data, size);
free(data);"
}
}
}
}

103
src/net/os/pci.jakt Normal file
View File

@@ -0,0 +1,103 @@
import ioport { IOPort }
import extern c "pci.h" {
extern fn pci_find(anon class_code: i64) -> i64
extern fn pci_read_u8(anon bus: i64, anon device: i64, anon fun: i64, anon offset: i64) -> u8
extern fn pci_read_u16(anon bus: i64, anon device: i64, anon fun: i64, anon offset: i64) -> u16
extern fn pci_read_u32(anon bus: i64, anon device: i64, anon fun: i64, anon offset: i64) -> u32
extern fn pci_write_u8(anon bus: i64, anon device: i64, anon fun: i64, anon offset: i64, anon value: u8)
extern fn pci_write_u16(anon bus: i64, anon device: i64, anon fun: i64, anon offset: i64, anon value: u16)
extern fn pci_write_u32(anon bus: i64, anon device: i64, anon fun: i64, anon offset: i64, anon value: u32)
}
struct PCIDevice {
bus: i64
device: i64
fun: i64
pci_vendor_id: u16
pci_device_id: u16
bar: [u32]
public fn enable_bus_master(mut this) {
.set_command(.command() | 0x4)
}
public fn read_u8(this, anon offset: i64) -> u8 {
return pci_read_u8(.bus, .device, .fun, offset)
}
public fn read_u16(this, anon offset: i64) -> u16 {
return pci_read_u16(.bus, .device, .fun, offset)
}
public fn read_u32(this, anon offset: i64) -> u32 {
return pci_read_u32(.bus, .device, .fun, offset)
}
public fn write_u8(this, offset: i64, value: u8) {
pci_write_u8(.bus, .device, .fun, offset, value)
}
public fn write_u16(this, offset: i64, value: u16) {
pci_write_u16(.bus, .device, .fun, offset, value)
}
public fn write_u32(this, offset: i64, value: u32) {
pci_write_u32(.bus, .device, .fun, offset, value)
}
public fn io_read_u8(this, anon offset: u16) throws -> u8 {
return IOPort::read_u8(.bar[0] as! u16 + offset)
}
public fn io_read_u16(this, anon offset: u16) throws -> u16 {
return IOPort::read_u16(.bar[0] as! u16 + offset)
}
public fn io_read_u32(this, anon offset: u16) throws -> u32 {
return IOPort::read_u32(.bar[0] as! u16 + offset)
}
public fn io_write_u8(this, offset: u16, value: u8) {
IOPort::write_u8(address: .bar[0] as! u16 + offset, value)
}
public fn io_write_u16(this, offset: u16, value: u16) {
IOPort::write_u16(address: .bar[0] as! u16 + offset, value)
}
public fn io_write_u32(this, offset: u16, value: u32) {
IOPort::write_u32(address: .bar[0] as! u16 + offset, value)
}
public fn vendor_id(this) -> u16 {
return .pci_vendor_id
}
public fn device_id(this) -> u16 {
return .pci_device_id
}
public fn command(this) -> u16 {
return pci_read_u16(.bus, .device, .fun, 0x4)
}
public fn set_command(this, anon value: u16) {
pci_write_u16(.bus, .device, .fun, offset: 0x4, value)
}
public fn status(this) -> u16 {
return pci_read_u16(.bus, .device, .fun, 0x6)
}
}
fn lookup_bar(bus: i64, device: i64, fun: i64, anon index: i64) -> u32 {
if index < 0 or index > 5 {
return 0xFFFFFFFF
}
return pci_read_u32(bus, device, fun, 0x10 + (index * 4)) & 0xFFFFFFFC
}
struct PCI {
public fn find_device_by_class_code(anon class_code: i64) throws -> PCIDevice {
let result = pci_find(class_code)
if result < 0 {
eprintln("error: device not found")
throw Error::from_errno(1)
}
let bus = (result >> 16) & 0xff
let device = (result >> 8) & 0xff
let fun = result & 0xff
let pci_vendor_id = pci_read_u16(bus, device, fun, 0x0)
let pci_device_id = pci_read_u16(bus, device, fun, 0x2)
mut bar: [u32] = []
for i in 0..5 {
bar.push(lookup_bar(bus, device, fun, i))
}
return PCIDevice(bus, device, fun, pci_vendor_id, pci_device_id, bar)
}
}

94
src/net/os/time.jakt Normal file
View File

@@ -0,0 +1,94 @@
import extern c "time.h" {
extern fn time_busy(anon duration: i64)
extern fn time_jiffies() -> i64
extern fn time_now() -> i64
extern fn time_sleep(anon duration: i64)
}
struct Time {
fn busy(anon duration: i64) {
time_busy(duration)
}
fn jiffies() throws -> i64 {
return time_jiffies()
}
fn now() throws -> i64 {
return time_now()
}
fn cdate_to_unix(anon cdate: i64) -> i64 {
// (cdate - Str2Date("1/1/1970") / CDATE_FREQ + NIST_TIME_OFFSET
return (cdate - 3090344933588992) / 49710 + 8575
}
fn unix_to_cdate(anon unix: i64) -> i64 {
// (unix - NIST_TIME_OFFSET) * CDATE_FREQ + Str2Date("1/1/1970")
return (unix - 8575) * 49710 + 3090344933588992
}
fn sleep(anon duration: i64) {
time_sleep(duration)
}
fn timestamp_from_unix(anon timestamp: i64) -> String {
let SECS_PER_DAY = 86400
let DAYS_PER_YEAR = 365
let DAYS_PER_LYEAR = 366
let DAYS_PER_LYEAR_PERIOD = 146097
let YEARS_PER_LYEAR_PERIOD = 400
mut days = timestamp / SECS_PER_DAY
mut remainder = timestamp - (days * SECS_PER_DAY)
if timestamp < 0 and remainder == 0 {
days++
remainder -= SECS_PER_DAY
}
mut cur_year = 0
mut months: [i64] = []
mut tmp_days = 0
let month_tab = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ]
let month_tab_leap = [ -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ]
tmp_days = days;
if tmp_days >= DAYS_PER_LYEAR_PERIOD or tmp_days <= -DAYS_PER_LYEAR_PERIOD {
cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
}
while tmp_days >= DAYS_PER_LYEAR {
cur_year++;
if cur_year % 4 == 0 {
tmp_days -= DAYS_PER_LYEAR;
} else {
tmp_days -= DAYS_PER_YEAR;
}
}
if cur_year % 4 == 0 {
months = month_tab_leap
} else {
months = month_tab
}
mut i = 11
while i > 0 {
if tmp_days > months[i] {
break;
}
i--
}
let year = 1970 + cur_year
let month = i + 1
let day = tmp_days - months[i]
let hours = remainder / 3600
let minutes = (remainder - hours * 3600) / 60
let seconds = remainder % 60
mut sb = StringBuilder::create()
sb.clear()
sb.appendff("{:0>4d}-{:0>2d}-{:0>2d}T{:0>2d}:{:0>2d}:{:0>2d}.000Z", year, month, day, hours, minutes, seconds)
return sb.to_string()
}
fn timestamp_from_cdate(anon cdate: i64) -> String {
return timestamp_from_unix(cdate_to_unix(cdate))
}
}