Skip to content

Commit

Permalink
Merge pull request sysprog21#305 from ChinYikMing/refactor_state_head…
Browse files Browse the repository at this point in the history
…er_file

Consolidate public header by integrating src/state.h
  • Loading branch information
jserv authored Dec 22, 2023
2 parents 3aab28b + 3c7835c commit c919d7c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 55 deletions.
1 change: 0 additions & 1 deletion src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extern struct target_ops gdbstub_ops;
#include "mpool.h"
#include "riscv.h"
#include "riscv_private.h"
#include "state.h"
#include "utils.h"

#if RV32_HAS(JIT)
Expand Down
2 changes: 1 addition & 1 deletion src/gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "mini-gdbstub/include/gdbstub.h"

#include "breakpoint.h"
#include "riscv.h"
#include "riscv_private.h"
#include "state.h"

static int rv_read_reg(void *args, int regno, size_t *data)
{
Expand Down
1 change: 1 addition & 0 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static uint8_t *data_memory_base;
memory_t *memory_new(void)
{
memory_t *mem = malloc(sizeof(memory_t));
assert(mem);
#if HAVE_MMAP
data_memory_base = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/jit_x64.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "decode.h"
#include "io.h"
#include "jit_x64.h"
#include "state.h"
#include "riscv.h"
#include "utils.h"

enum VM_REG {
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <unistd.h>

#include "elf.h"
#include "state.h"
#include "riscv.h"
#include "utils.h"

/* enable program trace mode */
Expand Down
29 changes: 27 additions & 2 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mpool.h"
#include "riscv.h"
#include "riscv_private.h"
#include "state.h"
#include "utils.h"
#if RV32_HAS(JIT)
#include "cache.h"
Expand Down Expand Up @@ -155,7 +156,6 @@ bool rv_enables_to_output_exit_code(riscv_t *rv)
return rv->output_exit_code;
}


void rv_delete(riscv_t *rv)
{
assert(rv);
Expand Down Expand Up @@ -293,3 +293,28 @@ void rv_reset(riscv_t *rv, riscv_word_t pc, int argc, char **args)

rv->halt = false;
}

state_t *state_new(void)
{
state_t *s = malloc(sizeof(state_t));
assert(s);
s->mem = memory_new();
s->break_addr = 0;

s->fd_map = map_init(int, FILE *, map_cmp_int);
FILE *files[] = {stdin, stdout, stderr};
for (size_t i = 0; i < ARRAYS_SIZE(files); i++)
map_insert(s->fd_map, &i, &files[i]);

return s;
}

void state_delete(state_t *s)
{
if (!s)
return;

map_delete(s->fd_map);
memory_delete(s->mem);
free(s);
}
22 changes: 22 additions & 0 deletions src/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
*/

#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "io.h"
#include "map.h"

#if RV32_HAS(EXT_F)
#define float16_t softfloat_float16_t
#define bfloat16_t softfloat_bfloat16_t
Expand Down Expand Up @@ -205,6 +210,23 @@ bool rv_has_halted(riscv_t *rv);
/* return the flag of outputting exit code */
bool rv_enables_to_output_exit_code(riscv_t *rv);

/* state structure passed to the runtime */
typedef struct {
memory_t *mem;

/* the data segment break address */
riscv_word_t break_addr;

/* file descriptor map: int -> (FILE *) */
map_t fd_map;
} state_t;

/* create a state */
state_t *state_new(void);

/* delete a state */
void state_delete(state_t *s);

#ifdef __cplusplus
};
#endif
47 changes: 0 additions & 47 deletions src/state.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdlib.h>
#include <sys/time.h>

#include "state.h"
#include "riscv.h"
#include "utils.h"

#define PREALLOC_SIZE 4096
Expand Down
2 changes: 1 addition & 1 deletion src/syscall_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <SDL.h>
#include <SDL_mixer.h>

#include "state.h"
#include "riscv.h"

/* The DSITMBK sound effect in DOOM1.WAD uses a sample rate of 22050, but since
* the game is played in single-player mode, it is acceptable to stick with
Expand Down

0 comments on commit c919d7c

Please sign in to comment.