1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![no_std]

mod display;
mod logger;
pub mod prelude;

use core::panic::PanicInfo;
use core::sync::atomic::{self, Ordering};
use display::Display;
use logger::Logger;
use mwatch_kernel::application::{Context, InputFn, ServiceFn, SetupFn, Table};
use mwatch_kernel::system::input::InputEvent;

#[link_section = ".entry_point"]
#[no_mangle]
/// First 4 bytes of the binary as a function ptr
/// The pointer the watch calls to start running this application.
pub static ENTRY_POINT: SetupFn = entry_point;

#[link_section = ".update_point"]
#[no_mangle]
/// Second 4 byte function ptr
/// The pointer the watch calls to start running this application.
pub static UPDATE_POINT: ServiceFn = update_point;

#[link_section = ".input_point"]
#[no_mangle]
/// Final 4 byte function ptr
/// The pointer the watch calls to handle input
pub static INPUT_POINT: InputFn = input_point;

extern "Rust" {
    fn main() -> i32;
    fn update(system: &mut UserSpace, display: &mut Display) -> i32;
    fn input(system: &mut UserSpace, input: InputEvent) -> i32;
}

#[no_mangle]
/// The function called by the host to start us up. Does some setup, then
/// jumps to a function called `main` defined by the actual application using
/// this crate.
pub unsafe extern "C" fn entry_point(table: *mut Table) -> i32 {
    MWABI.set_table(table);
    // TODO pass hw info here
    main()
}

#[no_mangle]
/// Calls the user update function
pub unsafe extern "C" fn update_point(raw_ctx: *mut Context) -> i32 {
    // Turn the pointer into a reference and store in a static.
    MWABI.set_context(raw_ctx);

    update(
        &mut UserSpace {
            logger: Logger::new(),
        },
        &mut Display::new(),
    )
}

#[no_mangle]
/// Calls the user update function
pub unsafe extern "C" fn input_point(raw_ctx: *mut Context, state: InputEvent) -> i32 {
    // Turn the pointer into a reference and store in a static.
    MWABI.set_context(raw_ctx);

    input(
        &mut UserSpace {
            logger: Logger::new(),
        },
        state,
    )
}

static mut MWABI: MWatchABI = MWatchABI {
    context: core::ptr::null_mut(),
    table: core::ptr::null_mut(),
};

pub(crate) struct MWatchABI {
    context: *mut Context,
    table: *mut Table,
}

impl MWatchABI {
    
    pub(crate) fn context() -> &'static mut Context {
        unsafe {
            debug_assert!(!MWABI.context.is_null());
            &mut *MWABI.context
        }
    }

    pub(crate) fn table() -> &'static mut Table {
        unsafe {
            debug_assert!(!MWABI.table.is_null());
            &mut *MWABI.table
        }
    }

    pub(crate) fn set_context(&mut self, context: *mut Context) {
        self.context = context;
    }

    pub(crate) fn set_table(&mut self, table: *mut Table) {
        self.table = table;
    }
}

#[repr(C)]
/// User space api abstraction
pub struct UserSpace {
    pub logger: Logger,
}

#[cfg(not(any(feature = "panic-log", feature = "panic-simple")))]
#[inline(never)]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {
        atomic::compiler_fence(Ordering::SeqCst);
    }
}

#[cfg(feature = "panic-log")]
#[inline(never)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    unsafe {
        UserSpace {
            logger: Logger::new(),
        }
        .logger
        .log_fmt(format_args!("{:?}", info))
    };

    loop {
        atomic::compiler_fence(Ordering::SeqCst);
    }
}

#[cfg(feature = "panic-simple")]
#[inline(never)]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    UserSpace {
        logger: Logger::new(),
    }
    .logger
    .log_str("application panicked");

    loop {
        atomic::compiler_fence(Ordering::SeqCst);
    }
}