Skip to content

General purpose allocator #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions application_processor/main.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const std = @import("std");

// const params = @cImport({
// @cInclude("ectf_params.h");
// });

const params = @import("params");
const shared = @import("shared");
const msdk = shared.msdk;
Expand All @@ -12,27 +8,40 @@ pub const std_options = .{
.logFn = shared.usb_log,
};

pub const os = struct {
pub const heap = struct {
pub const page_allocator = std.heap.c_allocator;
};
};

/// Entrypoint for the application processor
pub export fn main() noreturn {
std.log.info("Initializing AP", .{});
_ = msdk.LED_Init();

var gpa = std.heap.GeneralPurposeAllocator(.{ .enable_memory_limit = true }){
.requested_memory_limit = 0x0001e000,
};
const allocator = gpa.allocator();
const arr = allocator.alloc(i32, 10) catch unreachable;
std.log.info("{any}", .{arr});

initI2C() catch {
msdk.LED_On(msdk.LED1);
_ = msdk.MXC_Delay(msdk.MXC_DELAY_MSEC(5000));
};
msdk.LED_Off(msdk.LED1);

_ = params.AP_PIN;

sendI2C("Hello, world!") catch {
msdk.LED_On(msdk.LED3);
_ = msdk.MXC_Delay(msdk.MXC_DELAY_MSEC(5000));
};
// sendI2C("Hello, world!") catch {
// msdk.LED_On(msdk.LED3);
// _ = msdk.MXC_Delay(msdk.MXC_DELAY_MSEC(5000));
// };

msdk.LED_Off(msdk.LED1);
msdk.LED_Off(msdk.LED2);

while (true) {
std.log.info("2 + 2 = {any}", .{2 + 2});
var n: usize = 0;
while (true) : (n += 1) {
msdk.LED_Off(msdk.LED3);
msdk.LED_On(msdk.LED1);
_ = msdk.MXC_Delay(msdk.MXC_DELAY_MSEC(1000));
Expand All @@ -42,6 +51,10 @@ pub export fn main() noreturn {
msdk.LED_Off(msdk.LED2);
msdk.LED_On(msdk.LED3);
_ = msdk.MXC_Delay(msdk.MXC_DELAY_MSEC(1000));

if (n % @as(usize, 10) == 0) {
std.log.info("Total requested bytes: {}", .{gpa.total_requested_bytes});
}
}
}

Expand All @@ -52,20 +65,29 @@ const I2CError = error{

const I2C_ADDR = 0x50;
const I2C_INTERFACE = msdk.MXC_I2C1;
const I2C_SPEED = 10000;
const I2C_SPEED = 100000;

/// Initializes the I2C interface. Should be called exactly once
pub fn initI2C() !void {
msdk.MXC_ICC_Enable(msdk.MXC_ICC0);

_ = msdk.MXC_SYS_Clock_Select(msdk.MXC_SYS_CLOCK_IPO);
var err = msdk.MXC_SYS_Clock_Select(msdk.MXC_SYS_CLOCK_IPO);
if (err != msdk.E_SUCCESS) {
_ = msdk.printf("MXC_SYS_Clock_Select failed: %d\n", err);
return I2CError.InitFailed;
}

const success = msdk.MXC_I2C_Init(I2C_INTERFACE, 1, 0);
if (success != msdk.E_SUCCESS) {
err = msdk.MXC_I2C_Init(I2C_INTERFACE, 1, 0);
if (err != msdk.E_SUCCESS) {
_ = msdk.printf("MXC_I2C_Init failed: %d\n", err);
return I2CError.InitFailed;
}

_ = msdk.MXC_I2C_SetFrequency(I2C_INTERFACE, I2C_SPEED);
err = msdk.MXC_I2C_SetFrequency(I2C_INTERFACE, I2C_SPEED);
if (err < 0) {
_ = msdk.printf("MXC_I2C_SetFrequency failed: %d\n", err);
return I2CError.InitFailed;
}

msdk.MXC_I2C_SetTimeout(I2C_INTERFACE, 100);
}
Expand Down
8 changes: 7 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,25 @@ pub fn build(b: *std.Build) !void {
.single_threaded = true,
.target = target,
.name = "main",
.link_libc = true,
});

const compExe = b.addExecutable(.{
.root_source_file = b.path("component/main.zig"),
.single_threaded = true,
.target = target,
.name = "main",
.link_libc = true,
});

const sharedModule = b.createModule(.{
.root_source_file = b.path("shared/main.zig"),
});
sharedModule.addImport("msdk", msdk.createModule());

const msdkModule = msdk.createModule();
sharedModule.addImport("msdk", msdkModule);
apExe.root_module.addImport("msdk", msdkModule);
compExe.root_module.addImport("msdk", msdkModule);

apExe.root_module.addImport("shared", sharedModule);
compExe.root_module.addImport("shared", sharedModule);
Expand Down
16 changes: 15 additions & 1 deletion component/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ pub const std_options = .{
.logFn = shared.usb_log,
};

/// Entrypoint for the application processor
pub const os = struct {
pub const heap = struct {
pub const page_allocator = std.heap.c_allocator;
};
};

/// Entrypoint for the component
pub export fn main() noreturn {
std.log.info("Initializing Component", .{});
_ = msdk.LED_Init();

var gpa = std.heap.GeneralPurposeAllocator(.{ .enable_memory_limit = true }){
.requested_memory_limit = 0x0001e000,
};
const allocator = gpa.allocator();
_ = allocator;

msdk.LED_Off(msdk.LED1);
msdk.LED_Off(msdk.LED2);

Expand Down
56 changes: 53 additions & 3 deletions msdk_includes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
#include <stdint.h>
#include <stdio.h>
#include <led.h>
#include <mxc_delay.h>
#include <mxc.h>
#include <mxc_delay.h>
#include <stdint.h>
#include <stdio.h>

typedef struct {
__attribute__((aligned(16))) uint64_t lo;
int64_t hi;
} zig_i128;

zig_i128 __multi3(zig_i128 lhs, zig_i128 rhs) {
uint64_t a_low = lhs.lo;
int64_t a_high = lhs.hi;
uint64_t b_low = rhs.lo;
int64_t b_high = rhs.hi;

uint32_t a_ll = (uint32_t)a_low;
uint32_t a_lh = (uint32_t)(a_low >> 32);
uint32_t a_hl = (uint32_t)a_high;
uint32_t a_hh = (uint32_t)(a_high >> 32);
uint32_t b_ll = (uint32_t)b_low;
uint32_t b_lh = (uint32_t)(b_low >> 32);
uint32_t b_hl = (uint32_t)b_high;
uint32_t b_hh = (uint32_t)(b_high >> 32);

uint64_t p0 = (uint64_t)a_ll * b_ll;
uint64_t p1 = (uint64_t)a_ll * b_lh;
uint64_t p2 = (uint64_t)a_lh * b_ll;
uint64_t p3 = (uint64_t)a_lh * b_lh;
uint64_t p4 = (uint64_t)a_hl * b_ll;
uint64_t p5 = (uint64_t)a_hl * b_lh;
uint64_t p6 = (uint64_t)a_hh * b_ll;
uint64_t p7 = (uint64_t)a_hh * b_lh;

uint64_t result_low = p0;
uint64_t t = p1 + (p0 >> 32);
result_low = (t << 32) | (result_low & 0xFFFFFFFF);

uint64_t result_high = (t >> 32) + p2 + p3;
result_high += p4 + p5 + p6 + p7;

if (a_high < 0) {
result_high += (uint64_t)b_low;
}
if (b_high < 0) {
result_high += (uint64_t)a_low;
}

zig_i128 result;
result.lo = result_low;
result.hi = result_high;

return result;
}
Loading