mirror of
https://github.com/ringtailsoftware/uvm32.git
synced 2026-06-05 22:43:39 +00:00
Add console tetris demo
This commit is contained in:
parent
cd95e63af5
commit
c0711213ae
9 changed files with 261 additions and 0 deletions
49
apps/zigtris/src/console.zig
Normal file
49
apps/zigtris/src/console.zig
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
const std = @import("std");
|
||||
extern fn console_write(data: [*]const u8, len: usize) void;
|
||||
var wbuf:[4096]u8 = undefined;
|
||||
var cw = ConsoleWriter.init(&wbuf);
|
||||
const uvm = @import("uvm.zig");
|
||||
|
||||
pub const WriteError = error{ Unsupported, NotConnected };
|
||||
|
||||
pub const ConsoleWriter = struct {
|
||||
interface: std.Io.Writer,
|
||||
err: ?WriteError = null,
|
||||
|
||||
fn drain(w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize {
|
||||
var ret: usize = 0;
|
||||
|
||||
const b = w.buffered();
|
||||
uvm.print(b);
|
||||
_ = w.consume(b.len);
|
||||
|
||||
for (data) |d| {
|
||||
uvm.print(d);
|
||||
ret += d.len;
|
||||
}
|
||||
|
||||
const pattern = data[data.len - 1];
|
||||
for (0..splat) |_| {
|
||||
uvm.print(pattern);
|
||||
ret += pattern.len;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
pub fn init(buf: []u8) ConsoleWriter {
|
||||
return ConsoleWriter{
|
||||
.interface = .{
|
||||
.buffer = buf,
|
||||
.vtable = &.{
|
||||
.drain = drain,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub fn getWriter() *std.Io.Writer {
|
||||
return &cw.interface;
|
||||
}
|
||||
|
||||
41
apps/zigtris/src/main.zig
Normal file
41
apps/zigtris/src/main.zig
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
const uvm = @import("uvm.zig");
|
||||
|
||||
const mibu = @import("mibu");
|
||||
const zigtris = @import("zigtris");
|
||||
var nextEvent:mibu.events.Event = .none;
|
||||
|
||||
const console = @import("console.zig").getWriter();
|
||||
|
||||
export fn main() void {
|
||||
var gameRunning = true;
|
||||
var lastUpdate:u32 = 0;
|
||||
|
||||
zigtris.gamesetup(console, uvm.millis()) catch |err| {
|
||||
_ = console.print("err {any}\n", .{err}) catch 0;
|
||||
_ = console.flush() catch 0;
|
||||
return;
|
||||
};
|
||||
|
||||
while(gameRunning) {
|
||||
const now = uvm.millis();
|
||||
if (uvm.getch()) |key| {
|
||||
switch(key) {
|
||||
' ' => nextEvent = mibu.events.Event{.key = .{.code = .{.char = ' '}}},
|
||||
'a' => nextEvent = mibu.events.Event{.key = .{.code = .left}},
|
||||
'd' => nextEvent = mibu.events.Event{.key = .{.code = .right}},
|
||||
'w' => nextEvent = mibu.events.Event{.key = .{.code = .up}},
|
||||
's' => nextEvent = mibu.events.Event{.key = .{.code = .down}},
|
||||
'q' => gameRunning = false,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
if (now > lastUpdate + 100 or nextEvent != .none) {
|
||||
|
||||
lastUpdate = now;
|
||||
gameRunning = zigtris.gameloop(console, now, nextEvent) catch false;
|
||||
nextEvent = .none;
|
||||
_ = console.flush() catch 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
73
apps/zigtris/src/uvm.zig
Normal file
73
apps/zigtris/src/uvm.zig
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
const uvm32 = @cImport({
|
||||
@cDefine("USE_MAIN", "1");
|
||||
@cInclude("uvm32_target.h");
|
||||
});
|
||||
const std = @import("std");
|
||||
|
||||
// dupeZ would be better, but want to avoid using an allocator
|
||||
var new_buf:[128]u8 = undefined;
|
||||
pub inline fn print(m: []const u8) void {
|
||||
@memcpy(new_buf[0..m.len], m);
|
||||
new_buf[m.len] = 0;
|
||||
const s = new_buf[0..m.len :0];
|
||||
|
||||
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINT}) ++ ", %[arg1]"
|
||||
:
|
||||
: [arg1] "r" (s.ptr),
|
||||
);
|
||||
}
|
||||
|
||||
pub inline fn println(val: [:0]const u8) void {
|
||||
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTLN}) ++ ", %[arg1]"
|
||||
:
|
||||
: [arg1] "r" (val.ptr),
|
||||
);
|
||||
}
|
||||
|
||||
pub inline fn printd(val: u32) void {
|
||||
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTD}) ++ ", %[arg1]"
|
||||
:
|
||||
: [arg1] "r" (val),
|
||||
);
|
||||
}
|
||||
|
||||
pub inline fn printx(val: u32) void {
|
||||
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTX}) ++ ", %[arg1]"
|
||||
:
|
||||
: [arg1] "r" (val),
|
||||
);
|
||||
}
|
||||
|
||||
pub inline fn printc(val: u32) void {
|
||||
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_PRINTC}) ++ ", %[arg1]"
|
||||
:
|
||||
: [arg1] "r" (val),
|
||||
);
|
||||
}
|
||||
|
||||
pub inline fn yield() void {
|
||||
asm volatile (std.fmt.comptimePrint("csrwi 0x{x}, 0", .{uvm32.IOREQ_YIELD}));
|
||||
}
|
||||
|
||||
var millis_storage:u32 = 0;
|
||||
pub inline fn millis() u32 {
|
||||
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_MILLIS}) ++ ", %[arg1]"
|
||||
:
|
||||
: [arg1] "r" (&millis_storage),
|
||||
);
|
||||
return millis_storage;
|
||||
}
|
||||
|
||||
var getch_storage:u32 = 0;
|
||||
pub inline fn getch() ?u8 {
|
||||
asm volatile ("csrw " ++ std.fmt.comptimePrint("0x{x}", .{uvm32.IOREQ_GETC}) ++ ", %[arg1]"
|
||||
:
|
||||
: [arg1] "r" (&getch_storage),
|
||||
);
|
||||
if (getch_storage <= 0xFF) {
|
||||
return @truncate(getch_storage);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue