Blame

ebe99f Bob Green 2026-03-19 08:31:29
1
# Machine Library
2
3
I've created a library targeting the hardware specific aspects of programming the Sbc-3. I believe that it should work with the other boards from [Mega Micros](https://mega-micros.co.uk/index.htm), but since I don't have access to anything but the Sbc-3, can't confirm that.
4
c44439 Bob Green 2026-05-27 15:02:09
5
> [!WARNING]
6
> if compiling for the 68000, you'll need to change the target cpu in `src/stdenv.mk` and `src/lib/Makefile` by changing the **CPU** macro.
7
ebe99f Bob Green 2026-03-19 08:31:29
8
To use the library, just include the following in your C sourcecode:
9
```c
10
#include <machine.h>
11
```
2a20d4 Bob Green 2026-03-21 15:03:23
12
13
## Serial I/O
b26e81 Bob Green 2026-03-21 15:10:58
14
```C
15
int _char_available(void);
16
char _getchar(void);
17
void _putchar(char c);
18
```
6eb7d3 Bob Green 2026-05-27 15:01:14
19
20
> [!NOTE]
21
> The xr68681 is clocked at 7.3728MHz, meaning you'll get double the baud rate listed in the data sheet as the datasheet assumes half that rate. It will also affect the internal counter when it's clocked by the external clock.
22
23
----------
9601f3 Bob Green 2026-05-27 09:23:37
24
b26e81 Bob Green 2026-03-21 15:10:58
25
2a20d4 Bob Green 2026-03-21 15:03:23
26
## LED Control
27
There's a block of 10 leds (LD7 on the SBC3-b3), and six of them can be used for anything you like. Led1 is furthest from the cpu:
d90362 Bob Green 2026-03-21 15:03:37
28
2a20d4 Bob Green 2026-03-21 15:03:23
29
| LED # | Used for |
30
|:------|:---------|
31
| 1 | Indicates that the board is being powered via an external 5v supply. |
32
| 2 | Indicated that the board is being powered via the ATX power connector. |
33
| 3 | Connected to the ~RTS pin on the duart channel B. |
34
| 4 | Connected to the ~RTS pin on the duart channel A. |
35
| 5 | Available to user. |
36
| 6 | Available to user. |
37
| 7 | Available to user. |
38
| 8 | Available to user. |
39
| 9 | Available to user. |
40
| 10 | Available to user. |
91e139 Bob Green 2026-03-21 15:06:40
41
42
Two functions are provided, one to turn on a led, and another to turn it off.
43
44
```C
45
void set_led(int lednum);
46
void clear_led(int lednum);
47
```
48
Where `lednum` is an integer in the range of 5 through to 10, as per the table above. Any other value will be silently ignored.
0c5486 Bob Green 2026-03-24 15:10:57
49
b69dc8 Bob Green 2026-03-24 15:11:57
50
## PI/T
7a7f71 Bob Green 2026-05-27 09:36:47
51
The runtime startup (crt0) contains code which sets up an interrupt handler for the PIT timer and configures the counter to interrupt every millisecond.
b69dc8 Bob Green 2026-03-24 15:11:57
52
```C
53
void _pit_reset(void);
be80e9 Bob Green 2026-05-27 09:34:38
54
unsigned int pit_get_counter(void);
55
unsigned int pit_set_counter(unsigned int);
56
uint32_t ticks(void);
57
void idle_for_ticks(uint32);
58
3f2fd6 Bob Green 2026-05-27 08:42:03
59
void pit_set_a(uint8_t val);
60
void pit_set_bits_a(uint8_t bits);
61
void pit_clear_bits_a(uint8_t bits);
62
void pit_set_b(uint8_t val);
63
void pit_set_bits_b(uint8_t bits);
64
void pit_clear_bits_b(uint8_t bits);
b69dc8 Bob Green 2026-03-24 15:11:57
65
```
66
d3b3ce Bob Green 2026-05-27 16:45:37
67
> [!note]
7ab0b5 Bob Green 2026-05-27 16:45:05
68
> The PI/T is clocked at 10.0MHz
69
0c5486 Bob Green 2026-03-24 15:10:57
70
## Compact Flash
71
72
```C
73
void _cf_wait_busy(void);
74
void _cf_wait_data(void);
75
void cf_init(void);
910d81 Bob Green 2026-05-21 09:58:31
76
int cf_identify(uint8_t drive_num, cf_info_t *info);
0c5486 Bob Green 2026-03-24 15:10:57
77
int cf_read(uint8_t drive_num, uint32_t sector, uint8_t *buffer);
78
```
d83b67 Bob Green 2026-03-24 15:21:18
79
80
## Vectors and Interrupts
81
82
```C
9463ec Bob Green 2026-03-24 15:22:39
83
unsigned int *_get_vectors_base(void);
fc90ec Bob Green 2026-03-29 13:27:43
84
unsigned int get_isr_handler(int vector_number);
85
unsigned int set_isr_handler(int vector_number, void isr(void));
d83b67 Bob Green 2026-03-24 15:21:18
86
```
910d81 Bob Green 2026-05-21 09:58:31
87
1e0c28 Bob Green 2026-05-21 09:47:07
88
`_get_vectors_base()` return the contents of the `ivr` register on the 68030 or 0 on the 68000.
41d04d Bob Green 2026-05-21 09:47:20
89
1e0c28 Bob Green 2026-05-21 09:47:07
90
`get_isr_handler` returns the address of the handler currently configured to handle the exception specified by `vector_number`.
41d04d Bob Green 2026-05-21 09:47:20
91
f80b2d Bob Green 2026-05-21 09:49:31
92
`set_isr_handler()` sets a handler for the exception specified by `vector_number`. It returns the address of the handler that it replaced, or `0xffffffff` if the `vector_number` is out of range.
633040 Bob Green 2026-03-24 15:33:07
93
f01b84 Bob Green 2026-03-24 15:41:57
94
## `$I^2c$` Bus
633040 Bob Green 2026-03-24 15:33:07
95
96
```C
97
```
398ef5 Bob Green 2026-05-21 09:41:14
98
99
## Safe Memory Access
100
101
```C
102
int peek(uint8_t *addr);
103
int poke(uint8_t *addr, uint8_t val);
104
```
105
`peek()` attempts to read a byte from memory, returning the value read, or -1 if the read failed due to a Bus Error. Similarly, `poke()` attempts to write a byte to memory, returning -1 on a Bus Error.