Skip to content

Commit 57491c0

Browse files
committed
Move peripherals to devices directory
- modify Makefile to enable detect devices directory - decouple PLIC and UART into separate files - Bind RISC-V core to plic_t to enable sending interrupt from PLIC to core
1 parent cfb3c3a commit 57491c0

File tree

12 files changed

+137
-88
lines changed

12 files changed

+137
-88
lines changed

Makefile

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ OBJS_EXT :=
4747

4848
ifeq ($(call has, SYSTEM), 1)
4949
OBJS_EXT += system.o
50-
OBJS_EXT += plic.o
51-
OBJS_EXT += uart.o
5250
endif
5351

5452
# Integer Multiplication and Division instructions
@@ -230,11 +228,23 @@ ifeq ($(call has, GDBSTUB), 1)
230228
$(OBJS): $(GDBSTUB_LIB)
231229
endif
232230

231+
# Peripherals for system emulation
232+
ifeq ($(call has, SYSTEM), 1)
233+
DEV_OUT := $(OUT)/devices
234+
DEV_SRC := src/devices
235+
$(DEV_OUT)/%.o: $(DEV_SRC)/%.c $(deps_emcc)
236+
$(Q)mkdir -p $(DEV_OUT)
237+
$(VECHO) " CC\t$@\n"
238+
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_emcc) -c -MMD -MF $@.d $<
239+
DEV_OBJS := $(patsubst $(DEV_SRC)/%.c, $(DEV_OUT)/%.o, $(wildcard $(DEV_SRC)/*.c))
240+
deps += $(DEV_OBJS:%.o=%.o.d)
241+
endif
242+
233243
$(OUT)/%.o: src/%.c $(deps_emcc)
234244
$(VECHO) " CC\t$@\n"
235245
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_emcc) -c -MMD -MF $@.d $<
236246

237-
$(BIN): $(OBJS)
247+
$(BIN): $(OBJS) $(DEV_OBJS)
238248
$(VECHO) " LD\t$@\n"
239249
$(Q)$(CC) -o $@ $(CFLAGS_emcc) $^ $(LDFLAGS)
240250

@@ -331,7 +341,7 @@ endif
331341
endif
332342

333343
clean:
334-
$(RM) $(BIN) $(OBJS) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT) src/rv32_jit.c
344+
$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT) src/rv32_jit.c
335345
distclean: clean
336346
-$(RM) $(DOOM_DATA) $(QUAKE_DATA)
337347
$(RM) -r $(TIMIDITY_DATA)

src/plic.c renamed to src/devices/plic.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
/*
2+
* rv32emu is freely redistributable under the MIT License. See the file
3+
* "LICENSE" for information on usage and redistribution of this file.
4+
*/
5+
6+
#include <assert.h>
7+
#include <stdlib.h>
8+
19
#include "plic.h"
10+
#include "../riscv.h"
11+
#include "../riscv_private.h"
212

3-
void plic_update_interrupts(riscv_t *rv)
13+
void plic_update_interrupts(plic_t *plic)
414
{
5-
vm_attr_t *attr = PRIV(rv);
6-
plic_t *plic = attr->plic;
15+
riscv_t *rv = (riscv_t *) plic->rv;
716

817
/* Update pending interrupts */
918
plic->ip |= plic->active & ~plic->masked;
@@ -15,11 +24,8 @@ void plic_update_interrupts(riscv_t *rv)
1524
rv->csr_sip &= ~SIP_SEIP;
1625
}
1726

18-
uint32_t plic_read(riscv_t *rv, const uint32_t addr)
27+
uint32_t plic_read(plic_t *plic, const uint32_t addr)
1928
{
20-
vm_attr_t *attr = PRIV(rv);
21-
plic_t *plic = attr->plic;
22-
2329
/* no priority support: source priority hardwired to 1 */
2430
if (1 <= addr && addr <= 31)
2531
return 0;
@@ -54,11 +60,8 @@ uint32_t plic_read(riscv_t *rv, const uint32_t addr)
5460
return plic_read_val;
5561
}
5662

57-
void plic_write(riscv_t *rv, const uint32_t addr, uint32_t value)
63+
void plic_write(plic_t *plic, const uint32_t addr, uint32_t value)
5864
{
59-
vm_attr_t *attr = PRIV(rv);
60-
plic_t *plic = attr->plic;
61-
6265
/* no priority support: source priority hardwired to 1 */
6366
if (1 <= addr && addr <= 31)
6467
return;
@@ -81,3 +84,11 @@ void plic_write(riscv_t *rv, const uint32_t addr, uint32_t value)
8184

8285
return;
8386
}
87+
88+
plic_t *plic_new()
89+
{
90+
plic_t *plic = calloc(1, sizeof(plic_t));
91+
assert(plic);
92+
93+
return plic;
94+
}

src/devices/plic.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* rv32emu is freely redistributable under the MIT License. See the file
3+
* "LICENSE" for information on usage and redistribution of this file.
4+
*/
5+
6+
#pragma once
7+
8+
#include <stdint.h>
9+
10+
/* PLIC */
11+
typedef struct {
12+
uint32_t masked;
13+
uint32_t ip;
14+
uint32_t ie;
15+
/* state of input interrupt lines (level-triggered), set by environment */
16+
uint32_t active;
17+
/* RISC-V instance to receive PLIC interrupt */
18+
void *rv;
19+
} plic_t;
20+
21+
/* update PLIC status */
22+
void plic_update_interrupts(plic_t *plic);
23+
24+
/* read a word from PLIC */
25+
uint32_t plic_read(plic_t *plic, const uint32_t addr);
26+
27+
/* write a word to PLIC */
28+
void plic_write(plic_t *plic, const uint32_t addr, uint32_t value);
29+
30+
/* create a PLIC instance */
31+
plic_t *plic_new();

src/uart.c renamed to src/devices/uart.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
/*
2+
* rv32emu is freely redistributable under the MIT License. See the file
3+
* "LICENSE" for information on usage and redistribution of this file.
4+
*/
5+
16
#include <errno.h>
27
#include <poll.h>
38
#include <stdio.h>
49
#include <stdlib.h>
510
#include <string.h>
611
#include <unistd.h>
7-
8-
#include "io.h"
12+
#include <assert.h>
913

1014
/* Emulate 8250 (plain, without loopback mode support) */
1115

16+
#include "uart.h"
17+
1218
#define U8250_INT_THRE 1
1319

1420
void u8250_update_interrupts(u8250_state_t *uart)
@@ -142,3 +148,11 @@ void u8250_write(u8250_state_t *uart, uint32_t addr, uint32_t value)
142148
{
143149
u8250_reg_write(uart, addr, value);
144150
}
151+
152+
u8250_state_t *u8250_new()
153+
{
154+
u8250_state_t *uart = calloc(1, sizeof(u8250_state_t));
155+
assert(uart);
156+
157+
return uart;
158+
}

src/devices/uart.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* rv32emu is freely redistributable under the MIT License. See the file
3+
* "LICENSE" for information on usage and redistribution of this file.
4+
*/
5+
6+
#pragma once
7+
8+
#include <stdbool.h>
9+
#include <stdint.h>
10+
11+
#define IRQ_UART 1
12+
#define IRQ_UART_BIT (1 << IRQ_UART)
13+
14+
typedef struct {
15+
uint8_t dll, dlh; /* divisor (ignored) */
16+
uint8_t lcr; /* UART config */
17+
uint8_t ier; /* interrupt config */
18+
uint8_t current_int, pending_ints; /* interrupt status */
19+
uint8_t mcr; /* other output signals, loopback mode (ignored) */
20+
int in_fd, out_fd; /* I/O handling */
21+
bool in_ready;
22+
} u8250_state_t;
23+
24+
/* update UART status */
25+
void u8250_update_interrupts(u8250_state_t *uart);
26+
27+
/* poll UART status */
28+
void u8250_check_ready(u8250_state_t *uart);
29+
30+
/* read a word from UART */
31+
uint32_t u8250_read(u8250_state_t *uart, uint32_t addr);
32+
33+
/* write a word to UART */
34+
void u8250_write(u8250_state_t *uart, uint32_t addr, uint32_t value);
35+
36+
/* create a UART instance */
37+
u8250_state_t *u8250_new();

src/emulate.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
#include <stdlib.h>
1111
#include <string.h>
1212

13-
#if RV32_HAS(SYSTEM)
14-
#include "plic.h"
15-
#endif /* RV32_HAS(SYSTEM) */
16-
1713
#ifdef __EMSCRIPTEN__
1814
#include <emscripten.h>
1915
#endif

src/io.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@
1515

1616
#include "io.h"
1717

18-
u8250_state_t *u8250_new()
19-
{
20-
u8250_state_t *uart = calloc(1, sizeof(u8250_state_t));
21-
assert(uart);
22-
23-
return uart;
24-
}
25-
26-
plic_t *plic_new()
27-
{
28-
plic_t *plic = calloc(1, sizeof(plic_t));
29-
assert(plic);
30-
31-
return plic;
32-
}
33-
3418
static uint8_t *data_memory_base;
3519

3620
memory_t *memory_new(uint32_t size)

src/io.h

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,7 @@
99
#include <stdint.h>
1010
#include <string.h>
1111

12-
/* UART */
13-
14-
#define IRQ_UART 1
15-
#define IRQ_UART_BIT (1 << IRQ_UART)
16-
17-
typedef struct {
18-
uint8_t dll, dlh; /**< divisor (ignored) */
19-
uint8_t lcr; /**< UART config */
20-
uint8_t ier; /**< interrupt config */
21-
uint8_t current_int, pending_ints; /**< interrupt status */
22-
/* other output signals, loopback mode (ignored) */
23-
uint8_t mcr;
24-
/* I/O handling */
25-
int in_fd, out_fd;
26-
bool in_ready;
27-
} u8250_state_t;
28-
void u8250_update_interrupts(u8250_state_t *uart);
29-
void u8250_check_ready(u8250_state_t *uart);
30-
31-
uint32_t u8250_read(u8250_state_t *uart, uint32_t addr);
32-
33-
void u8250_write(u8250_state_t *uart, uint32_t addr, uint32_t value);
34-
35-
/* create a UART controller */
36-
u8250_state_t *u8250_new();
37-
38-
typedef struct {
39-
uint32_t masked;
40-
uint32_t ip;
41-
uint32_t ie;
42-
/* state of input interrupt lines (level-triggered), set by environment */
43-
uint32_t active;
44-
} plic_t;
45-
46-
/* create a PLIC core */
47-
plic_t *plic_new();
48-
12+
/* main memory */
4913
typedef struct {
5014
uint8_t *mem_base;
5115
uint64_t mem_size;

src/plic.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/riscv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@ riscv_t *rv_create(riscv_user_t rv_attr)
367367
/* setup PLIC */
368368
attr->plic = plic_new();
369369
assert(attr->plic);
370+
attr->plic->rv = rv;
370371

371-
/* setup PLIC */
372+
/* setup UART */
372373
attr->uart = u8250_new();
373374
assert(attr->uart);
374375
attr->uart->in_fd = 0;

0 commit comments

Comments
 (0)