refactor(kernel): 适配 ViperC 新语法

- 去掉不再需要的 import t, c
- class → struct(纯数据聚合)
- t.CArray[T, N] → [N]T
- static 修饰符移到类型前
- @__attribute(packed) → @__attribute__(packed)
- UHCI 中断回调参数改用 callable[[], int]
- 同步 CPUID 注释中的旧类型名
This commit is contained in:
TermiNexus committed 2026-10-26 22:39:56 +08:00
1 parent 89dd08895a
commit 3583f78efc
13 files changed
+41 -54

No files matched your search

+2 -1
View File
@@ -241,8 +241,9 @@ class _CPUObject:
info.ext_family = (res.eax >> 20) & 0xFF
# CPUID 品牌串:0x80000002..0x80000004 共 3 个叶 × 16 字节 = 48,
# 与 brand_string[48] 等长(此前 range(4) 会写 64 字节 → 越界 16 字节)。
# 逐 u32 按字节地址显式写入,避免 uint32|t.CPtr 被降级为 i8* 后
# 逐 u32 按字节地址显式写入,避免 uint32* 被降级为 i8* 后
# 下标按字节偏移且被截断为 i8(只写低字节、偏移不乘 4)。
# 存疑
brand_addr: uint64 = uint64(uint32*(info.brand_string))
for i in range(3):
cpuid(int(0x80000002 + i), &res)
@@ -1,6 +1,5 @@
from stdint import *
import fat32_types as types
import t, c
handle_table = types.handle_table
@@ -20,7 +20,7 @@ def _calc_fat_size(total_sectors: uint64, reserved: uint32, sec_per_clus: uint8,
return fat_sz
def _write_boot_sector(pdrv: uint8, total_sectors: uint64, sec_per_clus: uint8, vol_id: uint32, fat_sz: uint32) -> types.DRESULT:
buf: t.CArray[uint8, 512]
buf: [512]uint8
string.memset(&buf[0], 0, 512)
buf[0] = 0xEB; buf[1] = 0x58; buf[2] = 0x90
buf[3] = 'M'; buf[4] = 'S'; buf[5] = 'W'; buf[6] = 'I'; buf[7] = 'N'
@@ -45,7 +45,7 @@ def _write_boot_sector(pdrv: uint8, total_sectors: uint64, sec_per_clus: uint8,
buf[64] = 0x80
buf[66] = 0x29
types._write32(&buf[0], 67, vol_id)
vol_label: t.CArray[char, 11]
vol_label: [11]char
vol_label[0] = 'N'; vol_label[1] = 'O'; vol_label[2] = ' '; vol_label[3] = ' '
vol_label[4] = ' '; vol_label[5] = ' '; vol_label[6] = ' '; vol_label[7] = ' '
vol_label[8] = ' '; vol_label[9] = ' '; vol_label[10] = ' '
@@ -57,7 +57,7 @@ def _write_boot_sector(pdrv: uint8, total_sectors: uint64, sec_per_clus: uint8,
return diskio.write(pdrv, &buf[0], 0, 1)
def _write_fsinfo(pdrv: uint8, free_count: uint32, next_free: uint32) -> types.DRESULT:
buf: t.CArray[uint8, 512]
buf: [512]uint8
string.memset(&buf[0], 0, 512)
types._write32(&buf[0], 0, 0x41615252)
types._write32(&buf[0], 484, 0x61417272)
@@ -67,7 +67,7 @@ def _write_fsinfo(pdrv: uint8, free_count: uint32, next_free: uint32) -> types.D
return diskio.write(pdrv, &buf[0], 1, 1)
def _init_fat(pdrv: uint8, fat_start: uint64, fat_sz: uint32, root_cluster: uint32) -> types.DRESULT:
buf: t.CArray[uint8, 512]
buf: [512]uint8
string.memset(&buf[0], 0, 512)
types._write32(&buf[0], 0, 0x0FFFFFF8)
types._write32(&buf[0], 4, 0x0FFFFFFF)
@@ -88,7 +88,7 @@ def _init_fat(pdrv: uint8, fat_start: uint64, fat_sz: uint32, root_cluster: uint
return types.DRESULT.RES_OK
def _init_root_dir(pdrv: uint8, root_sector: uint64, sec_per_clus: uint8) -> types.DRESULT:
buf: t.CArray[uint8, 512]
buf: [512]uint8
string.memset(&buf[0], 0, 512)
s: uint8
for s in range(sec_per_clus):
@@ -124,10 +124,10 @@ def mkfs(pdrv: uint8, sec_per_clus: uint8, vol_id: uint32) -> types.FRESULT:
if res != types.DRESULT.RES_OK: return types.FRESULT.FR_DISK_ERR
res2: types.DRESULT = _write_fsinfo(pdrv, free_count, 3)
if res2 != types.DRESULT.RES_OK: return types.FRESULT.FR_DISK_ERR
boot_backup: t.CArray[uint8, 512]
boot_backup: [512]uint8
diskio.read(pdrv, &boot_backup[0], 0, 1)
diskio.write(pdrv, &boot_backup[0], 6, 1)
fsinfo_backup: t.CArray[uint8, 512]
fsinfo_backup: [512]uint8
diskio.read(pdrv, &fsinfo_backup[0], 1, 1)
diskio.write(pdrv, &fsinfo_backup[0], 7, 1)
fat_start: uint64 = uint64(reserved)
@@ -108,7 +108,7 @@ enum DRESULT:
RES_OK, RES_ERROR, RES_WRPRT, RES_NOTRDY, RES_PARERR
@__attribute(packed)
@__attribute__(packed)
class bpb:
BS_jmpBoot: [3]uint8
BS_OEMName: [8]char
@@ -2,8 +2,6 @@ import asm
import intr.idt as idt
import drivers.serial.uart.serial as serial
import platform.pch.pic as pic
import string
import t, c
define KBD_DATA = 0x60
@@ -18,19 +16,19 @@ define KEY_RELEASED = 0x80
# 读侧只做"取一个字节",不再在 read 里解码 —— 这样 has_data() 与 read_scancode()
# 看的是**同一个计数**,不可能出现"有数据但取不到"的自旋。
out_buf: [KBD_OUT_SIZE]uint8
out_head: static uint32 = 0
out_tail: static uint32 = 0
out_count: static uint32 = 0
kbd_irq_cnt: static uint64 = 0
static out_head: uint32 = 0
static out_tail: uint32 = 0
static out_count: uint32 = 0
static kbd_irq_cnt: uint64 = 0
# 硬件初始化只做一次(init 有多个调用点,见 init() 内注释)
_kbd_inited: static int = 0
static _kbd_inited: int = 0
shift_l: static uint8 = 0
shift_r: static uint8 = 0
ctrl_l: static uint8 = 0
alt_l: static uint8 = 0
caps_lock: static uint8 = 0
_e0_pending: static uint8 = 0
static shift_l: uint8 = 0
static shift_r: uint8 = 0
static ctrl_l: uint8 = 0
static alt_l: uint8 = 0
static caps_lock: uint8 = 0
static _e0_pending: uint8 = 0
scancode_set1: [128]char = [
'\0', '\x1b', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
+11 -13
View File
@@ -1,8 +1,6 @@
import asm
import intr.idt as idt
import platform.pch.pic as pic
import drivers.serial.uart.serial as serial
import t, c
define MOUSE_DATA = 0x60
@@ -22,19 +20,19 @@ class mouse_packet:
buttons: uint8
packets: [MOUSE_BUF_SIZE]mouse_packet
pack_head: static uint32 = 0
pack_tail: static uint32 = 0
pack_count: static uint32 = 0
mse_irq_cnt: static uint64 = 0
static pack_head: uint32 = 0
static pack_tail: uint32 = 0
static pack_count: uint32 = 0
static mse_irq_cnt: uint64 = 0
cycle: static uint8 = 0
byte1: static uint8 = 0
byte2: static uint8 = 0
byte3: static uint8 = 0
static cycle: uint8 = 0
static byte1: uint8 = 0
static byte2: uint8 = 0
static byte3: uint8 = 0
mouse_x: static int32 = 0
mouse_y: static int32 = 0
mouse_buttons: static uint8 = 0
static mouse_x: int32 = 0
static mouse_y: int32 = 0
static mouse_buttons: uint8 = 0
def _wait_read():
timeout: uint32 = 100000
@@ -1,6 +1,4 @@
import platform.pch as pch
import asm
import t, c
+1 -1
View File
@@ -178,7 +178,7 @@ def getDiskSize() -> uint64:
status: uint8 = asm.inb(0x1F7)
if not status & IDE_STATUS_DRDY: return 0
if status & IDE_STATUS_ERR: return 0
identify_data: t.CArray[uint16, 256]
identify_data: [256]uint16
for i in range(256):
identify_data[i] = asm.inw(0x1F0)
totalSectors: uint64 = identify_data[60] | uint64(identify_data[61]) << 16
+2 -3
View File
@@ -1,4 +1,3 @@
import t, c
import drivers.usb.usb as usb
import drivers.usb.hid.hid as hid
import drivers.usb.uhci as uhci
@@ -9,7 +8,7 @@ define KBD_BUF_SIZE = 1024
define KBD_EVT_PRESS = 0
define KBD_EVT_RELEASE = 1
class usb_kbd_event:
struct usb_kbd_event:
modifier: uint8
keycode: uint8
event_type: uint8
@@ -90,7 +89,7 @@ def init() -> int:
return -2
kbd_initialized = 1
serial.puts("[usb_kbd] initialized\n")
kbd_slot_id = uhci.register_int_callback(&_on_int_complete)
kbd_slot_id = uhci.register_int_callback(_on_int_complete)
if kbd_slot_id < 0:
serial.puts("[usb_kbd] register callback failed\n")
kbd_initialized = 0
+2 -3
View File
@@ -1,4 +1,3 @@
import t, c
import drivers.usb.usb as usb
import drivers.usb.hid.hid as hid
import drivers.usb.uhci as uhci
@@ -7,7 +6,7 @@ import drivers.serial.uart.serial as serial
define MOUSE_BUF_SIZE = 1024
class usb_mouse_packet:
struct usb_mouse_packet:
buttons: uint8
dx: int16
dy: int16
@@ -34,7 +33,7 @@ def init() -> int:
return -2
mse_initialized = 1
serial.puts("[usb_mse] initialized\n")
mse_slot_id = uhci.register_int_callback(&_on_int_complete)
mse_slot_id = uhci.register_int_callback(_on_int_complete)
if mse_slot_id < 0:
serial.puts("[usb_mse] register callback failed\n")
mse_initialized = 0
-1
View File
@@ -1,4 +1,3 @@
import t, c
import asm
+1 -2
View File
@@ -1,4 +1,3 @@
import t, c
import asm
import mm.mm as mm
import string
@@ -134,7 +133,7 @@ def _uhci_irq_handler() -> int:
ASM_DESCR.CLOBBER_RBP])
return 0
def register_int_callback(cb: int*) -> int:
def register_int_callback(cb: callable[[], int]) -> int:
global _int_slot_count
if _int_slot_count >= UHCI_INT_SLOTS:
return -1
+3 -6
View File
@@ -1,8 +1,5 @@
import t, c
@__attribute__(packed)
class gdt_entry:
struct gdt_entry:
limit_low: uint16
base_low: uint16
base_middle: uint8
@@ -11,7 +8,7 @@ class gdt_entry:
base_high: uint8
@__attribute__(packed)
class gdt_entry64:
struct gdt_entry64:
limit_low: uint16
base_low: uint16
base_middle: uint8
@@ -22,7 +19,7 @@ class gdt_entry64:
reserved: uint32
@__attribute__(packed)
class gdt_ptr:
struct gdt_ptr:
limit: uint16
base: uint64