Skip to content

Commit e25cc58

Browse files
committed
added test programs for file IO and updated readme and main
1 parent 2ab28dc commit e25cc58

File tree

6 files changed

+130
-7
lines changed

6 files changed

+130
-7
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ include_directories(
2121
core/machine
2222
core/common
2323
core/kernel
24+
core/filesystem
2425
)
2526

2627
# --- 3. Source Files ---
@@ -29,6 +30,7 @@ file(GLOB_RECURSE SOURCES
2930
"core/main.cpp"
3031
"core/machine/*.cpp"
3132
"core/kernel/*.cpp"
33+
"core/filesystem/*.cpp"
3234
)
3335

3436
# --- 4. Main Target (The Emulator) ---

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bash clean.sh
5959
```
6060
**Run**
6161
```bash
62-
./rv32kernel programs/<your_file.bin>
62+
./rv32kernel programs/<your_file.elf>
6363
```
6464

6565
## Example Output(Log)

core/main.cpp

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
1-
#include "Bus.hpp"
2-
#include "CPU.hpp"
31
#include "Kernel.hpp"
4-
#include "Memory.hpp"
2+
#include "Logger.hpp"
53
#include <iostream>
64
#include <string>
75

6+
#ifdef __EMSCRIPTEN__
7+
#include <emscripten/bind.h>
8+
using namespace emscripten;
9+
10+
void clearLogsWrapper()
11+
{
12+
CLEAR_LOGS();
13+
}
14+
void resetStatsWrapper()
15+
{
16+
STATS.reset();
17+
}
18+
19+
void flushLogsWrapper()
20+
{
21+
SHOW_LOGS();
22+
}
23+
24+
void printStatsWrapper()
25+
{
26+
STATS.printSummary();
27+
}
28+
EMSCRIPTEN_BINDINGS(kernel_module)
29+
{
30+
function("flushLogs", &flushLogsWrapper);
31+
function("printStats", &printStatsWrapper);
32+
function("clearLogs", &clearLogsWrapper);
33+
function("resetStats", &resetStatsWrapper);
34+
35+
class_<Kernel>("Kernel")
36+
.constructor<>()
37+
.function("createProcess", &Kernel::createProcess)
38+
.function("killProcess", &Kernel::killProcess)
39+
.function("init", &Kernel::init)
40+
.function("step", &Kernel::step)
41+
.function("isRunning", &Kernel::isRunning);
42+
}
43+
44+
int main()
45+
{
46+
return 0;
47+
}
48+
49+
#else
50+
851
int main(int argc, char* argv[])
952
{
1053
if (argc < 2)
1154
{
12-
std::cout << "Usage: ./rv32kernel <bin_file>\n";
55+
std::cout << "Usage: ./rv32kernel <elf_file>\n";
1356
return 1;
1457
}
1558

@@ -23,7 +66,16 @@ int main(int argc, char* argv[])
2366
std::cout << "Failed to create process.\n";
2467
return 1;
2568
}
26-
kernel.run();
2769

70+
kernel.init();
71+
72+
while (kernel.isRunning())
73+
kernel.step();
74+
75+
SHOW_LOGS();
76+
77+
STATS.printSummary();
2878
return 0;
29-
}
79+
}
80+
81+
#endif

lib/syscall.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,20 @@ void mutex_unlock(mutex_t* lock)
5252
if (lock == NULL) exit(-1);
5353
int status = syscall(SYS_MUTEX_UNLOCK, *lock);
5454
if (status == -1) exit(-1);
55+
}
56+
57+
int open(const char* filename)
58+
{
59+
return syscall(SYS_OPEN, (int)filename);
60+
}
61+
62+
int close(int fd)
63+
{
64+
return syscall(SYS_CLOSE, fd);
65+
}
66+
67+
int create(const char* filename, int sizeBytes)
68+
{
69+
if (sizeBytes <= 0) return -1;
70+
return syscall(SYS_CREATE, (int)filename, sizeBytes);
5571
}

lib/syscall.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#define SYS_MUTEX_LOCK 7
1515
#define SYS_MUTEX_UNLOCK 8
1616

17+
#define SYS_OPEN 9
18+
#define SYS_CLOSE 10
19+
#define SYS_CREATE 11
20+
1721
// mutex id
1822
typedef int mutex_t;
1923

@@ -30,4 +34,9 @@ mutex_t mutex_create();
3034
void mutex_lock(mutex_t* lock);
3135
void mutex_unlock(mutex_t* lock);
3236

37+
// file related
38+
int open(const char* filename);
39+
int close(int fd);
40+
int create(const char* filename, int sizeBytes);
41+
3342
#endif

programs/test11.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "stdio.h"
2+
#include "stdlib.h"
3+
#include "syscall.h"
4+
5+
int main()
6+
{
7+
// 1. Create a new file of size 1KB
8+
int ret = create("test.txt", 1024);
9+
10+
if (ret < 0)
11+
{
12+
putstr("Error creating file (Disk full or already exists).\n");
13+
return 1;
14+
}
15+
16+
// 2. Open it
17+
int fd = open("test.txt");
18+
19+
if (fd < 0)
20+
{
21+
putstr("Failed to open file!\n");
22+
return 1;
23+
}
24+
25+
// 3. Write to it (Write to RAM Disk)
26+
char msg[] = "Hello World From Doing File I/O!\n";
27+
unsigned int str_len = strlen(msg);
28+
write(fd, msg, str_len);
29+
30+
// 4. Seek back to start (You need lseek syscall for this, or close/reopen)
31+
close(fd);
32+
33+
fd = open("test.txt");
34+
// 5. Read back (Read from RAM Disk)
35+
char buf[100];
36+
int bytes_read = read(fd, buf, str_len);
37+
if (bytes_read > 0) buf[bytes_read] = '\0';
38+
39+
// 6. Print to Console (Write to Host Stdout)
40+
putstr(buf); // Should print "Hello Disk!"
41+
putstr("\n");
42+
43+
return 0;
44+
}

0 commit comments

Comments
 (0)