Make CPU code thread safe
This commit is contained in:
parent
afc68c1c96
commit
c878dee7e0
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
|||||||
NAME=vriscv
|
NAME=vriscv
|
||||||
CC=gcc
|
CC=gcc
|
||||||
CFLAGS=-O3 -Wall -I src
|
CFLAGS=-O3 -Wall -I src
|
||||||
LDFLAGS=
|
LDFLAGS=-lpthread
|
||||||
BUILD_DIR=build
|
BUILD_DIR=build
|
||||||
|
|
||||||
C_FILES := $(shell find src/ -name '*.c')
|
C_FILES := $(shell find src/ -name '*.c')
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
rv32_cpu_t* cpu0;
|
rv32_cpu_t* cpu0;
|
||||||
|
pthread_mutex_t* cpu0_mutex;
|
||||||
|
|
||||||
typedef union RAW_INSTRUCTION
|
typedef union RAW_INSTRUCTION
|
||||||
{
|
{
|
||||||
@ -40,6 +41,7 @@ static void cpu_print_instruction(instruction_t* instruction);
|
|||||||
void cpu_init()
|
void cpu_init()
|
||||||
{
|
{
|
||||||
cpu0 = calloc(1, sizeof(rv32_cpu_t));
|
cpu0 = calloc(1, sizeof(rv32_cpu_t));
|
||||||
|
pthread_mutex_init(cpu0_mutex, 0);
|
||||||
cpu0->regs.zero = 0;
|
cpu0->regs.zero = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -645,6 +647,9 @@ void cpu_loop(rv32_cpu_t* cpu)
|
|||||||
{
|
{
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
// Aquire CPU mutex
|
||||||
|
pthread_mutex_lock(cpu0_mutex);
|
||||||
|
|
||||||
// Fetch
|
// Fetch
|
||||||
raw_instruction_t raw_instruction;
|
raw_instruction_t raw_instruction;
|
||||||
if(cpu->pc > memory_size - 4)
|
if(cpu->pc > memory_size - 4)
|
||||||
@ -668,6 +673,9 @@ void cpu_loop(rv32_cpu_t* cpu)
|
|||||||
cpu->regs.zero = 0;
|
cpu->regs.zero = 0;
|
||||||
|
|
||||||
cpu->pc += 4;
|
cpu->pc += 4;
|
||||||
|
|
||||||
|
// Let go of cpu mutex
|
||||||
|
pthread_mutex_unlock(cpu0_mutex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define RV32CPU_H
|
#define RV32CPU_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is a structure encoding for the registers of
|
* This is a structure encoding for the registers of
|
||||||
@ -188,6 +189,7 @@ typedef struct RV32_CPU
|
|||||||
} rv32_cpu_t;
|
} rv32_cpu_t;
|
||||||
|
|
||||||
extern rv32_cpu_t* cpu0;
|
extern rv32_cpu_t* cpu0;
|
||||||
|
extern pthread_mutex_t* cpu0_mutex;
|
||||||
void cpu_init();
|
void cpu_init();
|
||||||
void cpu_loop(rv32_cpu_t* cpu);
|
void cpu_loop(rv32_cpu_t* cpu);
|
||||||
|
|
||||||
|
@ -183,6 +183,9 @@ void gdbstub_thread_gdb()
|
|||||||
// g : read all registers -> send back all registers
|
// g : read all registers -> send back all registers
|
||||||
char resp[32 * 8 + 8 + 1] = {0};
|
char resp[32 * 8 + 8 + 1] = {0};
|
||||||
|
|
||||||
|
// Obtain CPU0 mutex
|
||||||
|
pthread_mutex_lock(cpu0_mutex);
|
||||||
|
|
||||||
// All general purpose registers in host byte order as chars
|
// All general purpose registers in host byte order as chars
|
||||||
for(size_t i = 0; i < 32; i++)
|
for(size_t i = 0; i < 32; i++)
|
||||||
{
|
{
|
||||||
@ -194,6 +197,9 @@ void gdbstub_thread_gdb()
|
|||||||
uint32_t pc = htonl(cpu0->pc);
|
uint32_t pc = htonl(cpu0->pc);
|
||||||
snprintf(resp + 32 * 8, 9, "%08x", pc);
|
snprintf(resp + 32 * 8, 9, "%08x", pc);
|
||||||
|
|
||||||
|
// Let go of CPU0 mutex
|
||||||
|
pthread_mutex_unlock(cpu0_mutex);
|
||||||
|
|
||||||
// Final packet size, send packet
|
// Final packet size, send packet
|
||||||
size_t size = 32 * 8 + 8;
|
size_t size = 32 * 8 + 8;
|
||||||
gdbstub_send_packet(resp, size);
|
gdbstub_send_packet(resp, size);
|
||||||
@ -202,6 +208,9 @@ void gdbstub_thread_gdb()
|
|||||||
{
|
{
|
||||||
// G : write all registers -> read and set all registers
|
// G : write all registers -> read and set all registers
|
||||||
|
|
||||||
|
// Obtain CPU0 mutex
|
||||||
|
pthread_mutex_lock(cpu0_mutex);
|
||||||
|
|
||||||
// All general purpose registers in host byte order as chars
|
// All general purpose registers in host byte order as chars
|
||||||
for(size_t i = 1; i < 32; i++)
|
for(size_t i = 1; i < 32; i++)
|
||||||
{
|
{
|
||||||
@ -218,6 +227,9 @@ void gdbstub_thread_gdb()
|
|||||||
pc = ntohl(pc);
|
pc = ntohl(pc);
|
||||||
cpu0->pc = pc;
|
cpu0->pc = pc;
|
||||||
|
|
||||||
|
// Let go of CPU0 Mutex
|
||||||
|
pthread_mutex_unlock(cpu0_mutex);
|
||||||
|
|
||||||
gdbstub_send_packet("OK", 2);
|
gdbstub_send_packet("OK", 2);
|
||||||
}
|
}
|
||||||
else if(packet[0] == 'm')
|
else if(packet[0] == 'm')
|
||||||
|
Loading…
Reference in New Issue
Block a user