Formatting updates.

This commit is contained in:
Christoffer Lerno
2025-02-27 20:09:15 +01:00
committed by Christoffer Lerno
parent 8f0de40b3d
commit fbac2d6df3
7 changed files with 58 additions and 62 deletions

View File

@@ -3,9 +3,10 @@ import semihost;
const UART0_BASE = 0x10000000; const UART0_BASE = 0x10000000;
fn void main() @export("main") { fn void main() @export("main")
Uart* uart0 = (Uart*)UART0_BASE; // Create pointer to UART 0 {
Uart* uart0 = (Uart*)UART0_BASE; // Create pointer to UART 0
uart0.fcr = uart::UARTFCR_FFENA; // Enable FIFO uart0.fcr = uart::UARTFCR_FFENA; // Enable FIFO
uart0.puts("Hello World!\n"); // Write the string to the UART uart0.puts("Hello World!\n"); // Write the string to the UART
semihost::exit(0); // Semihosting call to exit host semihost::exit(0); // Semihosting call to exit host
} }

View File

@@ -4,7 +4,8 @@ module semihost;
extern fn int sys_semihost(int operation, SemihostParameters* parms); extern fn int sys_semihost(int operation, SemihostParameters* parms);
struct SemihostParameters { struct SemihostParameters
{
int field1; int field1;
int field2; int field2;
} }
@@ -12,7 +13,8 @@ struct SemihostParameters {
const int SYS_EXIT_EXTENDED = 0x20; const int SYS_EXIT_EXTENDED = 0x20;
const int ADP_STOPPED_APPLICATIONEXIT = 0x20026; const int ADP_STOPPED_APPLICATIONEXIT = 0x20026;
fn void exit(int status) { fn void exit(int status)
{
SemihostParameters parms; SemihostParameters parms;
parms.field1 = ADP_STOPPED_APPLICATIONEXIT; parms.field1 = ADP_STOPPED_APPLICATIONEXIT;
parms.field2 = status; parms.field2 = status;

View File

@@ -3,7 +3,8 @@ module uart;
const UARTFCR_FFENA = 0x01; // UART FIFO Control Register enable bit const UARTFCR_FFENA = 0x01; // UART FIFO Control Register enable bit
const UARTLSR_THRE = 0x20; // UART Line Status Register Transmit Hold Register Empty bit const UARTLSR_THRE = 0x20; // UART Line Status Register Transmit Hold Register Empty bit
struct Uart { struct Uart
{
char dr; // UART Data Register char dr; // UART Data Register
char filler1; char filler1;
char fcr; // FIFO Control Register char fcr; // FIFO Control Register
@@ -12,17 +13,21 @@ struct Uart {
char lsr; // Line Status Register char lsr; // Line Status Register
} }
fn bool Uart.uart_ff_thr_empty(Uart* this) { fn bool Uart.uart_ff_thr_empty(Uart* this)
{
return (bool)($$volatile_load(&this.lsr) & UARTLSR_THRE); return (bool)($$volatile_load(&this.lsr) & UARTLSR_THRE);
} }
fn void Uart.putc(Uart* this, char c) { fn void Uart.putc(Uart* this, char c)
while (!this.uart_ff_thr_empty()); // Wait until the FIFO holding register is empty {
$$volatile_store(&this.dr, c); // Write character to transmit register while (!this.uart_ff_thr_empty()); // Wait until the FIFO holding register is empty
$$volatile_store(&this.dr, c); // Write character to transmit register
} }
fn void Uart.puts(Uart* this, char *str) { fn void Uart.puts(Uart* this, char *str)
while (*str) { // Loop until value at string pointer is zero {
this.putc(*str++); // Write the character and increment pointer while (*str) // Loop until value at string pointer is zero
} {
this.putc(*str++); // Write the character and increment pointer
}
} }

View File

@@ -1,8 +1,8 @@
fn int main() fn int main()
{ {
String msg = "Hello, C3 World!\n"; String msg = "Hello, C3 World!\n";
$$syscall(1, 1, (uptr)msg.ptr, msg.len); // __NR_write, STDOUT $$syscall(1, 1, (uptr)msg.ptr, msg.len); // __NR_write, STDOUT
return 0; return 0;
} }
fn void _start() @export("_start") fn void _start() @export("_start")

View File

@@ -2,13 +2,15 @@ module gl;
def BitField = int; def BitField = int;
enum BufferBit : int(int value) { enum BufferBit : int (int value)
{
COLOR = 0x00004000, COLOR = 0x00004000,
STENCIL = 0x00000400, STENCIL = 0x00000400,
DEPTH = 0x00000100, DEPTH = 0x00000100,
} }
enum Primitive : int(int value) { enum Primitive : int (int value)
{
POINTS = 0, POINTS = 0,
LINES = 1, LINES = 1,
LINE_LOOP = 2, LINE_LOOP = 2,
@@ -29,8 +31,6 @@ extern fn void end() @extern("glEnd") @public;
extern fn void flush() @extern("glFlush") @public; extern fn void flush() @extern("glFlush") @public;
extern fn void color3f(float r, float g, float b) extern fn void color3f(float r, float g, float b) @extern("glColor3f") @public;
@extern("glColor3f") @public;
extern fn void vertex3f(float x, float y, float z) extern fn void vertex3f(float x, float y, float z) @extern("glVertex3f") @public;
@extern("glVertex3f") @public;

View File

@@ -2,40 +2,33 @@ module glfw;
import std::io; import std::io;
distinct Window @public = _Window*; distinct Window @public = inline _Window*;
distinct Monitor @public = _Monitor*; distinct Monitor @public = inline _Monitor*;
fn void Window.create( fn void Window.create(&self, int width, int height, String title, Monitor monitor = {}, Window share = {}
&self, ) @public
int width, {
int height, *self = (Window)_glfwCreateWindow(width, height, title, monitor, share);
String title,
Monitor monitor = {},
Window share = {}
) @public {
*self = (Window)_glfwCreateWindow(
width,
height,
title,
(_Monitor*)monitor,
(_Window*)share
);
} }
fn void Window.destroy(self) @public { fn void Window.destroy(self) @public
_glfwDestroyWindow((_Window*)self); {
_glfwDestroyWindow((_Window*)self);
} }
fn bool Window.shouldClose(self) @public { fn bool Window.shouldClose(self) @public
return _glfwWindowShouldClose((_Window*)self); {
return _glfwWindowShouldClose((_Window*)self);
} }
fn void Window.swapBuffers(self) @public { fn void Window.swapBuffers(self) @public
return _glfwSwapBuffers((_Window*)self); {
return _glfwSwapBuffers((_Window*)self);
} }
fn void Window.makeContextCurrent(self) @public { fn void Window.makeContextCurrent(self) @public
return _glfwMakeContextCurrent((_Window*)self); {
return _glfwMakeContextCurrent((_Window*)self);
} }
extern fn void initialize() @extern("glfwInit") @public; extern fn void initialize() @extern("glfwInit") @public;
@@ -47,18 +40,12 @@ extern fn void pollEvents() @extern("glfwPollEvents") @public;
distinct _Window @private = void; distinct _Window @private = void;
distinct _Monitor @private = void; distinct _Monitor @private = void;
extern fn _Window* _glfwCreateWindow( extern fn _Window* _glfwCreateWindow(int width, int height, char* title, _Monitor* monitor, _Window* share) @extern("glfwCreateWindow") @private;
int width, int height, char* title, _Monitor* monitor, _Window* share
) @extern("glfwCreateWindow") @private;
extern fn bool _glfwWindowShouldClose(_Window* window) extern fn bool _glfwWindowShouldClose(_Window* window) @extern("glfwWindowShouldClose") @private;
@extern("glfwWindowShouldClose") @private;
extern fn void _glfwDestroyWindow(_Window* window) extern fn void _glfwDestroyWindow(_Window* window) @extern("glfwDestroyWindow") @private;
@extern("glfwDestroyWindow") @private;
extern fn void _glfwSwapBuffers(_Window* window) extern fn void _glfwSwapBuffers(_Window* window) @extern("glfwSwapBuffers") @private;
@extern("glfwSwapBuffers") @private;
extern fn void _glfwMakeContextCurrent(_Window* window) extern fn void _glfwMakeContextCurrent(_Window* window) @extern("glfwMakeContextCurrent") @private;
@extern("glfwMakeContextCurrent") @private;

View File

@@ -2,16 +2,17 @@ import std::io;
import glfw; import glfw;
import gl; import gl;
fn void main() { fn void main()
{
glfw::initialize(); glfw::initialize();
Window window; Window window;
window.create(1280, 720, "Triangle example"); window.create(1280, 720, "Triangle example");
window.makeContextCurrent(); window.makeContextCurrent();
while(!window.shouldClose()) { while (!window.shouldClose())
{
gl::clear(BufferBit.COLOR.value); gl::clear(BufferBit.COLOR.value);
gl::begin(Primitive.TRIANGLES.value); gl::begin(Primitive.TRIANGLES.value);
gl::color3f(1, 0, 0); gl::vertex3f(-0.6, -0.75, 0.5); gl::color3f(1, 0, 0); gl::vertex3f(-0.6, -0.75, 0.5);
gl::color3f(0, 1, 0); gl::vertex3f(0.6, -0.75, 0); gl::color3f(0, 1, 0); gl::vertex3f(0.6, -0.75, 0);