emulator: Load Radiance images
87a22d95a2522b9d6090455d55ebed2b42d0d6da00306dc1894f50f00f83acdf
Hosted mode loaded only RAD0 version-1 images and headerless binaries with optional .ro.data and .rw.data sidecars. It could not load the native image format of current compilers. Hosted text was also limited to 4 MiB, less than the read-only address region it lives in. Load native images in hosted mode. The format is a 48-byte little- endian header, the magic "RADIANCE" followed by an address and size for the code and read-only data and an address, size and zero-filled tail for read-write data, then the bytes of the three segments. Execution starts at the code address. The loader checks only what protects the emulator: each segment, including the tail, lies in its region (code and read-only data below the read-write data base, read-write data within the data memory), and the file is exactly 48 bytes plus the three sizes. Memory past the stored bytes is not written, since guest memory starts out zero. The legacy RAD0 version-1 format still loads. Headerless binaries and the data sidecars are no longer supported, and unknown images are rejected. Text may fill the read-only address region and must not overlap writable data. A .debug sidecar still gives source locations. Drop code the emulator does not use: the F extension decoding and the instruction encoder in riscv.[ch], the -trace-headless alias of -trace, and -watch-disable. The README describes both image formats and the supported extensions. Assisted-by: Claude:sonnet-5.5 Assisted-by: Claude:opus-5.5 Assisted-by: Codex:gpt-6
1 parent
064c3426
README
+22 -12
| 2 | 2 | RADIANT RISC-V EMULATOR |
|
| 3 | 3 | ||
| 4 | 4 | A RISC-V RV64I emulator with an x86-64 JIT compiler, interactive TUI |
|
| 5 | 5 | debugger, and headless execution mode. Part of the Radiant project. |
|
| 6 | 6 | ||
| 7 | - | Supports the RV64I base integer instruction set, M (multiply/divide) |
|
| 8 | - | extension, and F (single-precision floating-point) extension. |
|
| 7 | + | Supports the RV64I base integer instruction set and the M (multiply/divide) |
|
| 8 | + | extension. |
|
| 9 | 9 | ||
| 10 | 10 | BUILDING |
|
| 11 | 11 | ||
| 12 | 12 | $ make |
|
| 13 | 13 |
| 28 | 28 | ||
| 29 | 29 | Headless: |
|
| 30 | 30 | ||
| 31 | 31 | $ bin/emulator -run <program.bin> [<option>..] |
|
| 32 | 32 | ||
| 33 | - | The emulator loads a self-contained Radiance RV64 image: |
|
| 33 | + | The emulator loads two image formats. The native format of current |
|
| 34 | + | compilers has a 48-byte little-endian header followed by the bytes of |
|
| 35 | + | three segments placed at their own addresses: |
|
| 34 | 36 | ||
| 35 | - | Header "RAD0", version, and section sizes |
|
| 36 | - | Text Machine instructions |
|
| 37 | - | Read-only data Loaded at 0x00010000 |
|
| 38 | - | Read-write data Loaded at 0x00FFFFF0 |
|
| 37 | + | 0 "RADIANCE" |
|
| 38 | + | 8 code address (u64), size (u32) |
|
| 39 | + | 20 read-only data address (u64), size (u32) |
|
| 40 | + | 32 read-write data address (u64), size (u32), zero-filled tail (u32) |
|
| 41 | + | 48 code, read-only data and read-write data bytes |
|
| 39 | 42 | ||
| 40 | - | Legacy flat binaries with optional `.ro.data` and `.rw.data` sidecars remain |
|
| 41 | - | supported. A `<program>.debug` sidecar may be used for source locations. |
|
| 43 | + | Execution starts at the code address. Code and read-only data occupy |
|
| 44 | + | exactly their size. Read-write data is followed by its zero-filled tail. |
|
| 45 | + | Each segment must lie inside the memory of the mode, and the file must be |
|
| 46 | + | exactly 48 plus the three sizes. Nothing else is checked. |
|
| 47 | + | ||
| 48 | + | The legacy format of older compilers, "RAD0" version 1, has section sizes |
|
| 49 | + | and fixed load addresses (text, then read-only data at 0x00010000, then |
|
| 50 | + | read-write data at 0x00FFFFF0). |
|
| 51 | + | ||
| 52 | + | A `<program>.debug` sidecar may be used for source locations. |
|
| 42 | 53 | ||
| 43 | 54 | ||
| 44 | 55 | OPTIONS |
|
| 45 | 56 | ||
| 46 | 57 | -run Run headless (no TUI). |
| 50 | 61 | -data-size=KB Data memory size in KB. |
|
| 51 | 62 | -stack-size=KB Stack size in KB (default 256 KB). |
|
| 52 | 63 | -no-guard-stack Disable stack guard zones (enabled by default, 16 bytes). |
|
| 53 | 64 | -no-validate Disable memory bounds checking. |
|
| 54 | 65 | -trace Enable instruction tracing. |
|
| 55 | - | -trace-headless Enable instruction tracing in headless mode. |
|
| 56 | 66 | -trace-instructions Print each instruction during headless tracing. |
|
| 57 | 67 | -trace-depth=N Number of trace entries to display on fault. |
|
| 58 | 68 | -max-steps=N Maximum steps before timeout in headless mode. |
|
| 59 | 69 | -count-instructions Print instruction count on exit. |
|
| 60 | 70 | -watch=ADDR Set a memory watchpoint at ADDR. |
| 65 | 75 | -watch-backtrace Print backtrace on watchpoint hit. |
|
| 66 | 76 | -watch-bt-depth=N Backtrace depth (default 8). |
|
| 67 | 77 | ||
| 68 | 78 | MEMORY LAYOUT |
|
| 69 | 79 | ||
| 70 | - | 0x00010000 Read-only data (.ro.data) |
|
| 80 | + | 0x00010000 Read-only data |
|
| 71 | 81 | Program base Program text (instructions) |
|
| 72 | - | 0x00FFFFF0 Read-write data (.rw.data) |
|
| 82 | + | 0x00FFFFF0 Read-write data |
|
| 73 | 83 | Memory top - stack size Stack |
|
| 74 | 84 | Memory top Top of memory |
|
| 75 | 85 | ||
| 76 | 86 | ARCHITECTURE |
|
| 77 | 87 |
emulator.c
+161 -184
| 39 | 39 | /* Maximum physical memory size (384MB). The runtime can choose any size up to |
|
| 40 | 40 | * this value with `-memory-size=...`. */ |
|
| 41 | 41 | #define MEMORY_SIZE (384 * 1024 * 1024) |
|
| 42 | 42 | /* Default physical memory size (128MB). */ |
|
| 43 | 43 | #define DEFAULT_MEMORY_SIZE (128 * 1024 * 1024) |
|
| 44 | - | /* Program memory size (4MB), reserved at start of memory for program code. */ |
|
| 45 | - | #define PROGRAM_SIZE (4 * 1024 * 1024) |
|
| 46 | 44 | /* Writable data region base. */ |
|
| 47 | 45 | #define DATA_RW_OFFSET 0xFFFFF0 |
|
| 48 | 46 | /* Data memory starts at writable data region. */ |
|
| 49 | 47 | #define DATA_MEMORY_START DATA_RW_OFFSET |
|
| 50 | 48 | /* Default data memory size. */ |
| 73 | 71 | /* TTY escape codes. */ |
|
| 74 | 72 | #define TTY_CLEAR "\033[2J\033[H" |
|
| 75 | 73 | #define TTY_GOTO_RC "\033[%d;%dH" |
|
| 76 | 74 | /* Exit code returned on EBREAK. */ |
|
| 77 | 75 | #define EBREAK_EXIT_CODE 133 |
|
| 78 | - | /* Self-contained Radiance image header. */ |
|
| 79 | - | #define IMAGE_MAGIC 0x30444152U |
|
| 80 | - | #define IMAGE_VERSION 1U |
|
| 81 | - | #define IMAGE_HEADER_SIZE 20U |
|
| 76 | + | /* Native image magic: the ASCII bytes "RADIANCE" as a little-endian u64. */ |
|
| 77 | + | #define NATIVE_IMAGE_MAGIC 0x45434E4149444152ULL |
|
| 78 | + | /* Legacy image magic of older compilers: the ASCII bytes "RAD0". A u32 |
|
| 79 | + | * version follows; only version 1 is supported. */ |
|
| 80 | + | #define LEGACY_IMAGE_MAGIC 0x30444152U |
|
| 81 | + | #define LEGACY_IMAGE_VERSION 1U |
|
| 82 | + | /* Size of the legacy image header. */ |
|
| 83 | + | #define LEGACY_HEADER_SIZE 20U |
|
| 82 | 84 | ||
| 83 | 85 | /* Registers displayed in the TUI, in order. */ |
|
| 84 | 86 | static const reg_t registers_displayed[] = { |
|
| 85 | 87 | SP, FP, RA, A0, A1, A2, A3, A4, A5, A6, A7, T0, T1, T2, T3, T4, T5, T6 |
|
| 86 | 88 | }; |
| 134 | 136 | static u8 memory[MEMORY_SIZE]; |
|
| 135 | 137 | ||
| 136 | 138 | /* Loaded section sizes, used for bounds checking and diagnostics. */ |
|
| 137 | 139 | static u32 program_base = 0; |
|
| 138 | 140 | static u32 program_bytes = 0; |
|
| 139 | - | static u32 rodata_bytes = 0; |
|
| 140 | 141 | static u32 data_bytes = 0; |
|
| 141 | 142 | ||
| 142 | 143 | /* Snapshot buffer. */ |
|
| 143 | 144 | static struct snapshot_buffer snapshots; |
|
| 144 | 145 |
| 502 | 503 | static int usage(const char *prog) { |
|
| 503 | 504 | fprintf( |
|
| 504 | 505 | stderr, |
|
| 505 | 506 | "usage: %s [-run] [-no-guard-stack]" |
|
| 506 | 507 | " [-stack-size=KB] [-no-validate] [-debug]" |
|
| 507 | - | " [-trace|-trace-headless] [-trace-depth=n] [-trace-instructions]" |
|
| 508 | + | " [-trace] [-trace-depth=n] [-trace-instructions]" |
|
| 508 | 509 | " [-max-steps=n] [-memory-size=KB] [-data-size=KB]" |
|
| 509 | 510 | " [-watch=addr] [-watch-size=bytes] [-watch-arm-pc=addr]" |
|
| 510 | 511 | " [-watch-zero-only] [-watch-skip=n]" |
|
| 511 | 512 | " [-watch-backtrace] [-watch-bt-depth=n]" |
|
| 512 | 513 | " [-count-instructions] [-no-jit]" |
| 704 | 705 | if (strcmp(arg, "-debug") == 0) { |
|
| 705 | 706 | g_opts.debug_enabled = true; |
|
| 706 | 707 | argi++; |
|
| 707 | 708 | continue; |
|
| 708 | 709 | } |
|
| 709 | - | if (strcmp(arg, "-trace") == 0 || strcmp(arg, "-trace-headless") == 0) { |
|
| 710 | + | if (strcmp(arg, "-trace") == 0) { |
|
| 710 | 711 | g_opts.trace_enabled = true; |
|
| 711 | 712 | argi++; |
|
| 712 | 713 | continue; |
|
| 713 | 714 | } |
|
| 714 | 715 | if (strcmp(arg, "-trace-instructions") == 0) { |
| 766 | 767 | if (!parse_u32(arg + 12, "watch skip", 0, &g_opts.watch_skip)) |
|
| 767 | 768 | return false; |
|
| 768 | 769 | argi++; |
|
| 769 | 770 | continue; |
|
| 770 | 771 | } |
|
| 771 | - | if (strcmp(arg, "-watch-disable") == 0) { |
|
| 772 | - | g_opts.watch_enabled = false; |
|
| 773 | - | argi++; |
|
| 774 | - | continue; |
|
| 775 | - | } |
|
| 776 | 772 | if (strncmp(arg, "-watch-arm-pc=", 14) == 0) { |
|
| 777 | 773 | if (!parse_u32(arg + 14, "watch arm pc", 0, &g_opts.watch_arm_pc)) |
|
| 778 | 774 | return false; |
|
| 779 | 775 | argi++; |
|
| 780 | 776 | continue; |
| 1253 | 1249 | return result; |
|
| 1254 | 1250 | } |
|
| 1255 | 1251 | return -1; |
|
| 1256 | 1252 | } |
|
| 1257 | 1253 | ||
| 1258 | - | /* Load a binary section from disk into emulator memory at the given offset. */ |
|
| 1259 | - | static u32 load_section( |
|
| 1260 | - | const char *filepath, |
|
| 1261 | - | const char *suffix, |
|
| 1262 | - | u32 offset, |
|
| 1263 | - | u32 limit, |
|
| 1264 | - | const char *label |
|
| 1265 | - | ) { |
|
| 1266 | - | char path[PATH_MAX]; |
|
| 1267 | - | snprintf(path, sizeof(path), "%s.%s", filepath, suffix); |
|
| 1268 | - | ||
| 1269 | - | FILE *file = fopen(path, "rb"); |
|
| 1270 | - | if (!file) |
|
| 1271 | - | return 0; |
|
| 1272 | - | ||
| 1273 | - | if (fseek(file, 0, SEEK_END) != 0) { |
|
| 1274 | - | fclose(file); |
|
| 1275 | - | bail("failed to seek %s section", label); |
|
| 1276 | - | } |
|
| 1277 | - | long size = ftell(file); |
|
| 1278 | - | if (size < 0) { |
|
| 1279 | - | fclose(file); |
|
| 1280 | - | bail("failed to determine size of %s section", label); |
|
| 1281 | - | } |
|
| 1282 | - | if (fseek(file, 0, SEEK_SET) != 0) { |
|
| 1283 | - | fclose(file); |
|
| 1284 | - | bail("failed to rewind %s section", label); |
|
| 1285 | - | } |
|
| 1286 | - | if (size == 0) { |
|
| 1287 | - | fclose(file); |
|
| 1288 | - | return 0; |
|
| 1289 | - | } |
|
| 1290 | - | ||
| 1291 | - | u32 u_size = (u32)size; |
|
| 1292 | - | u64 end = (u64)offset + (u64)u_size; |
|
| 1293 | - | if (end > (u64)limit) { |
|
| 1294 | - | fclose(file); |
|
| 1295 | - | u32 max_size = limit - offset; |
|
| 1296 | - | bail( |
|
| 1297 | - | "%s section too large for emulator memory: required %u bytes, max " |
|
| 1298 | - | "%u bytes", |
|
| 1299 | - | label, |
|
| 1300 | - | u_size, |
|
| 1301 | - | max_size |
|
| 1302 | - | ); |
|
| 1303 | - | } |
|
| 1304 | - | if (end > (u64)g_opts.memory_size) { |
|
| 1305 | - | fclose(file); |
|
| 1306 | - | u32 max_size = g_opts.memory_size - offset; |
|
| 1307 | - | bail( |
|
| 1308 | - | "%s section exceeds physical memory: required %u bytes at offset " |
|
| 1309 | - | "%u, " |
|
| 1310 | - | "but only %u bytes available (total memory=%u, use " |
|
| 1311 | - | "-memory-size=... or recompile emulator with a larger " |
|
| 1312 | - | "MEMORY_SIZE)", |
|
| 1313 | - | label, |
|
| 1314 | - | u_size, |
|
| 1315 | - | offset, |
|
| 1316 | - | max_size, |
|
| 1317 | - | g_opts.memory_size |
|
| 1318 | - | ); |
|
| 1319 | - | } |
|
| 1320 | - | size_t read = fread(&memory[offset], 1, u_size, file); |
|
| 1321 | - | fclose(file); |
|
| 1322 | - | ||
| 1323 | - | if (read != u_size) { |
|
| 1324 | - | bail( |
|
| 1325 | - | "could not read entire %s section: read %zu bytes, expected %u " |
|
| 1326 | - | "bytes (offset=%u, limit=%u)", |
|
| 1327 | - | label, |
|
| 1328 | - | read, |
|
| 1329 | - | u_size, |
|
| 1330 | - | offset, |
|
| 1331 | - | limit |
|
| 1332 | - | ); |
|
| 1333 | - | } |
|
| 1334 | - | return u_size; |
|
| 1335 | - | } |
|
| 1336 | - | ||
| 1337 | 1254 | /* Decode a little-endian word without relying on host alignment. */ |
|
| 1338 | 1255 | static u32 decode_u32_le(const u8 *bytes) { |
|
| 1339 | 1256 | return (u32)bytes[0] | ((u32)bytes[1] << 8) | ((u32)bytes[2] << 16) | |
|
| 1340 | 1257 | ((u32)bytes[3] << 24); |
|
| 1341 | 1258 | } |
|
| 1342 | 1259 | ||
| 1260 | + | /* Decode a little-endian doubleword without relying on host alignment. */ |
|
| 1261 | + | static u64 decode_u64_le(const u8 *bytes) { |
|
| 1262 | + | return (u64)decode_u32_le(bytes) | ((u64)decode_u32_le(bytes + 4) << 32); |
|
| 1263 | + | } |
|
| 1264 | + | ||
| 1343 | 1265 | /* Read exactly size bytes from file into guest memory at offset. */ |
|
| 1344 | 1266 | static void load_image_section( |
|
| 1345 | 1267 | FILE *file, u32 offset, u32 size, const char *label |
|
| 1346 | 1268 | ) { |
|
| 1347 | 1269 | if (size == 0) |
| 1372 | 1294 | u32 slice_array_size = (u32)argc * slice_size; |
|
| 1373 | 1295 | u32 base_size = slice_size + slice_array_size; |
|
| 1374 | 1296 | u32 total_size = align(base_size + (u32)bytes, 16); |
|
| 1375 | 1297 | ||
| 1376 | 1298 | /* Place the env block below the current stack pointer so it doesn't |
|
| 1377 | - | * overlap with uninitialized static data. The .rw.data file only |
|
| 1299 | + | * overlap with uninitialized static data. The image only |
|
| 1378 | 1300 | * contains initialized statics; undefined statics occupy memory after |
|
| 1379 | - | * the loaded data but are not in the file. Placing the env block in |
|
| 1301 | + | * the loaded data but are not in the image. Placing the env block in |
|
| 1380 | 1302 | * the data region would clobber those zero-initialized areas. */ |
|
| 1381 | 1303 | u32 sp = (u32)cpu->regs[SP]; |
|
| 1382 | 1304 | u32 env_addr = (sp - total_size) & ~0xFu; |
|
| 1383 | 1305 | ||
| 1384 | 1306 | if (env_addr <= DATA_MEMORY_START + data_bytes) |
| 1500 | 1422 | fclose(file); |
|
| 1501 | 1423 | ||
| 1502 | 1424 | return line; |
|
| 1503 | 1425 | } |
|
| 1504 | 1426 | ||
| 1505 | - | /* Load the program binary and data sections into memory. */ |
|
| 1427 | + | /* A native image segment: its address, the bytes stored in the file, and the |
|
| 1428 | + | * zero-filled bytes that follow them in memory. */ |
|
| 1429 | + | struct image_segment { |
|
| 1430 | + | u64 base; |
|
| 1431 | + | u32 size; |
|
| 1432 | + | u32 tail; |
|
| 1433 | + | }; |
|
| 1434 | + | ||
| 1435 | + | /* A decoded native image: the code, read-only data and read-write data |
|
| 1436 | + | * segments, in that order. Execution starts at the code address. */ |
|
| 1437 | + | struct image { |
|
| 1438 | + | struct image_segment segments[3]; |
|
| 1439 | + | }; |
|
| 1440 | + | ||
| 1441 | + | /* The address range [low, high) that each segment must lie in. */ |
|
| 1442 | + | struct image_range { |
|
| 1443 | + | u64 low[3]; |
|
| 1444 | + | u64 high[3]; |
|
| 1445 | + | }; |
|
| 1446 | + | ||
| 1447 | + | /* Decode and check a native image of file_size bytes, leaving the file |
|
| 1448 | + | * positioned at the payload. The caller has already checked the magic. */ |
|
| 1449 | + | static struct image parse_image( |
|
| 1450 | + | FILE *file, long file_size, const struct image_range *range |
|
| 1451 | + | ) { |
|
| 1452 | + | enum { HEADER_BYTES = 48 }; |
|
| 1453 | + | u8 header[HEADER_BYTES]; |
|
| 1454 | + | rewind(file); |
|
| 1455 | + | if (fread(header, 1, sizeof(header), file) != sizeof(header)) |
|
| 1456 | + | bail("truncated image header"); |
|
| 1457 | + | ||
| 1458 | + | struct image image; |
|
| 1459 | + | u64 expected = HEADER_BYTES; |
|
| 1460 | + | for (u32 i = 0; i < 3; i++) { |
|
| 1461 | + | const u8 *record = header + 8 + i * 12; |
|
| 1462 | + | struct image_segment *seg = &image.segments[i]; |
|
| 1463 | + | seg->base = decode_u64_le(record); |
|
| 1464 | + | seg->size = decode_u32_le(record + 8); |
|
| 1465 | + | seg->tail = i == 2 ? decode_u32_le(record + 12) : 0; |
|
| 1466 | + | expected += seg->size; |
|
| 1467 | + | u64 extent = (u64)seg->size + seg->tail; |
|
| 1468 | + | if (extent && |
|
| 1469 | + | (seg->base < range->low[i] || seg->base >= range->high[i] || |
|
| 1470 | + | extent > range->high[i] - seg->base)) |
|
| 1471 | + | bail("image segment outside memory"); |
|
| 1472 | + | } |
|
| 1473 | + | if (expected != (u64)file_size) |
|
| 1474 | + | bail("image size does not match header"); |
|
| 1475 | + | return image; |
|
| 1476 | + | } |
|
| 1477 | + | ||
| 1478 | + | /* Read the stored bytes of each segment into memory, with ram_base the address |
|
| 1479 | + | * that memory offset 0 stands for. The zero-filled tails need no clearing: |
|
| 1480 | + | * guest memory is static and the image loads once. */ |
|
| 1481 | + | static void load_image_segments( |
|
| 1482 | + | FILE *file, const struct image *image, u64 ram_base |
|
| 1483 | + | ) { |
|
| 1484 | + | for (u32 i = 0; i < 3; i++) { |
|
| 1485 | + | const struct image_segment *seg = &image->segments[i]; |
|
| 1486 | + | load_image_section( |
|
| 1487 | + | file, (u32)(seg->base - ram_base), seg->size, "native" |
|
| 1488 | + | ); |
|
| 1489 | + | } |
|
| 1490 | + | } |
|
| 1491 | + | ||
| 1492 | + | /* Load a native image in hosted mode and point the CPU at the code. Code and |
|
| 1493 | + | * read-only data must lie in [DATA_RO_OFFSET, DATA_MEMORY_START) and |
|
| 1494 | + | * read-write data in [DATA_MEMORY_START, data_limit). */ |
|
| 1495 | + | static void load_hosted_image( |
|
| 1496 | + | struct cpu *cpu, FILE *file, long file_size, u32 data_limit |
|
| 1497 | + | ) { |
|
| 1498 | + | struct image_range range = { |
|
| 1499 | + | .low = { DATA_RO_OFFSET, DATA_RO_OFFSET, DATA_MEMORY_START }, |
|
| 1500 | + | .high = { DATA_MEMORY_START, DATA_MEMORY_START, data_limit }, |
|
| 1501 | + | }; |
|
| 1502 | + | struct image image = parse_image(file, file_size, &range); |
|
| 1503 | + | load_image_segments(file, &image, 0); |
|
| 1504 | + | ||
| 1505 | + | const struct image_segment *code = &image.segments[0]; |
|
| 1506 | + | const struct image_segment *rwdata = &image.segments[2]; |
|
| 1507 | + | u32 extent = rwdata->size + rwdata->tail; |
|
| 1508 | + | program_base = (u32)code->base; |
|
| 1509 | + | program_bytes = code->size; |
|
| 1510 | + | /* The environment block must lie beyond the end of read-write data. */ |
|
| 1511 | + | data_bytes = extent ? (u32)(rwdata->base - DATA_MEMORY_START) + extent : 0; |
|
| 1512 | + | cpu->programsize = program_bytes / sizeof(instr_t); |
|
| 1513 | + | cpu->pc = (u32)code->base; |
|
| 1514 | + | } |
|
| 1515 | + | ||
| 1516 | + | /* Load the program image into memory. */ |
|
| 1506 | 1517 | static void program_init(struct cpu *cpu, const char *filepath) { |
|
| 1507 | 1518 | program_bytes = 0; |
|
| 1508 | 1519 | data_bytes = 0; |
|
| 1509 | 1520 | if (g_opts.debug_enabled) |
|
| 1510 | 1521 | debug_load(filepath); |
| 1528 | 1539 | ||
| 1529 | 1540 | u32 data_limit = DATA_MEMORY_START + g_opts.data_memory_size; |
|
| 1530 | 1541 | if (data_limit > g_opts.memory_size) |
|
| 1531 | 1542 | data_limit = g_opts.memory_size; |
|
| 1532 | 1543 | ||
| 1533 | - | u8 header[IMAGE_HEADER_SIZE]; |
|
| 1544 | + | u8 header[LEGACY_HEADER_SIZE]; |
|
| 1534 | 1545 | size_t header_read = fread(header, 1, sizeof(header), file); |
|
| 1535 | - | bool is_image = |
|
| 1536 | - | header_read >= sizeof(u32) && decode_u32_le(header) == IMAGE_MAGIC; |
|
| 1537 | - | ||
| 1538 | - | if (is_image) { |
|
| 1539 | - | if (header_read != sizeof(header)) { |
|
| 1540 | - | fclose(file); |
|
| 1541 | - | bail("truncated Radiance image header"); |
|
| 1542 | - | } |
|
| 1543 | - | ||
| 1544 | - | u32 version = decode_u32_le(&header[4]); |
|
| 1545 | - | program_bytes = decode_u32_le(&header[8]); |
|
| 1546 | - | rodata_bytes = decode_u32_le(&header[12]); |
|
| 1547 | - | data_bytes = decode_u32_le(&header[16]); |
|
| 1548 | - | ||
| 1549 | - | if (version != IMAGE_VERSION) { |
|
| 1550 | - | fclose(file); |
|
| 1551 | - | bail("unsupported Radiance image version: %u", version); |
|
| 1552 | - | } |
|
| 1553 | - | if (program_bytes == 0 || program_bytes % sizeof(instr_t) != 0) { |
|
| 1554 | - | fclose(file); |
|
| 1555 | - | bail( |
|
| 1556 | - | "invalid text section size in Radiance image: %u", program_bytes |
|
| 1557 | - | ); |
|
| 1558 | - | } |
|
| 1559 | - | u64 image_size = |
|
| 1560 | - | (u64)IMAGE_HEADER_SIZE + program_bytes + rodata_bytes + data_bytes; |
|
| 1561 | - | if (image_size != (u64)size) { |
|
| 1562 | - | fclose(file); |
|
| 1563 | - | bail( |
|
| 1564 | - | "Radiance image size does not match header: file has %ld " |
|
| 1565 | - | "bytes, header requires %llu", |
|
| 1566 | - | size, |
|
| 1567 | - | (unsigned long long)image_size |
|
| 1568 | - | ); |
|
| 1569 | - | } |
|
| 1570 | - | if (program_bytes > PROGRAM_SIZE) { |
|
| 1571 | - | fclose(file); |
|
| 1572 | - | bail( |
|
| 1573 | - | "text section too large: %u bytes; maximum is %u bytes", |
|
| 1574 | - | program_bytes, |
|
| 1575 | - | PROGRAM_SIZE |
|
| 1576 | - | ); |
|
| 1577 | - | } |
|
| 1578 | - | if ((u64)DATA_RO_OFFSET + rodata_bytes > DATA_MEMORY_START) { |
|
| 1579 | - | fclose(file); |
|
| 1580 | - | bail("read-only data section exceeds available program memory"); |
|
| 1581 | - | } |
|
| 1582 | - | program_base = align(DATA_RO_OFFSET + rodata_bytes, WORD_SIZE); |
|
| 1583 | - | if ((u64)program_base + program_bytes > DATA_MEMORY_START) { |
|
| 1584 | - | fclose(file); |
|
| 1585 | - | bail("text section exceeds available program memory"); |
|
| 1586 | - | } |
|
| 1587 | - | if ((u64)DATA_MEMORY_START + data_bytes > data_limit) { |
|
| 1588 | - | fclose(file); |
|
| 1589 | - | bail("read-write data section exceeds available data memory"); |
|
| 1590 | - | } |
|
| 1591 | - | ||
| 1592 | - | load_image_section(file, program_base, program_bytes, "text"); |
|
| 1593 | - | load_image_section( |
|
| 1594 | - | file, DATA_RO_OFFSET, rodata_bytes, "read-only data" |
|
| 1595 | - | ); |
|
| 1596 | - | load_image_section( |
|
| 1597 | - | file, DATA_MEMORY_START, data_bytes, "read-write data" |
|
| 1598 | - | ); |
|
| 1599 | - | } else { |
|
| 1600 | - | /* Legacy flat binaries use optional sidecar data files. */ |
|
| 1601 | - | rewind(file); |
|
| 1602 | - | rodata_bytes = load_section( |
|
| 1603 | - | filepath, "ro.data", DATA_RO_OFFSET, DATA_MEMORY_START, "ro.data" |
|
| 1604 | - | ); |
|
| 1605 | - | program_base = align(DATA_RO_OFFSET + rodata_bytes, WORD_SIZE); |
|
| 1606 | - | if (size > PROGRAM_SIZE) { |
|
| 1607 | - | fclose(file); |
|
| 1608 | - | bail( |
|
| 1609 | - | "invalid file size: %ld; maximum program size is %d bytes", |
|
| 1610 | - | size, |
|
| 1611 | - | PROGRAM_SIZE |
|
| 1612 | - | ); |
|
| 1613 | - | } |
|
| 1614 | - | if ((u64)program_base + (u32)size > DATA_MEMORY_START) { |
|
| 1615 | - | fclose(file); |
|
| 1616 | - | bail("text section exceeds available program memory"); |
|
| 1617 | - | } |
|
| 1618 | - | program_bytes = (u32)size; |
|
| 1619 | - | load_image_section(file, program_base, program_bytes, "program"); |
|
| 1620 | - | data_bytes = load_section( |
|
| 1621 | - | filepath, "rw.data", DATA_MEMORY_START, data_limit, "rw.data" |
|
| 1546 | + | if (header_read >= sizeof(u64) && |
|
| 1547 | + | decode_u64_le(header) == NATIVE_IMAGE_MAGIC) { |
|
| 1548 | + | load_hosted_image(cpu, file, size, data_limit); |
|
| 1549 | + | fclose(file); |
|
| 1550 | + | return; |
|
| 1551 | + | } |
|
| 1552 | + | if (header_read < sizeof(u32) || |
|
| 1553 | + | decode_u32_le(header) != LEGACY_IMAGE_MAGIC) { |
|
| 1554 | + | fclose(file); |
|
| 1555 | + | bail("unrecognized Radiance image: '%s'", filepath); |
|
| 1556 | + | } |
|
| 1557 | + | if (header_read != sizeof(header)) { |
|
| 1558 | + | fclose(file); |
|
| 1559 | + | bail("truncated Radiance image header"); |
|
| 1560 | + | } |
|
| 1561 | + | u32 version = decode_u32_le(&header[4]); |
|
| 1562 | + | if (version != LEGACY_IMAGE_VERSION) { |
|
| 1563 | + | fclose(file); |
|
| 1564 | + | bail("unsupported Radiance image version: %u", version); |
|
| 1565 | + | } |
|
| 1566 | + | program_bytes = decode_u32_le(&header[8]); |
|
| 1567 | + | u32 rodata_bytes = decode_u32_le(&header[12]); |
|
| 1568 | + | data_bytes = decode_u32_le(&header[16]); |
|
| 1569 | + | if (program_bytes == 0 || program_bytes % sizeof(instr_t) != 0) { |
|
| 1570 | + | fclose(file); |
|
| 1571 | + | bail("invalid text section size in Radiance image: %u", program_bytes); |
|
| 1572 | + | } |
|
| 1573 | + | u64 image_size = |
|
| 1574 | + | (u64)LEGACY_HEADER_SIZE + program_bytes + rodata_bytes + data_bytes; |
|
| 1575 | + | if (image_size != (u64)size) { |
|
| 1576 | + | fclose(file); |
|
| 1577 | + | bail( |
|
| 1578 | + | "Radiance image size does not match header: file has %ld " |
|
| 1579 | + | "bytes, header requires %llu", |
|
| 1580 | + | size, |
|
| 1581 | + | (unsigned long long)image_size |
|
| 1622 | 1582 | ); |
|
| 1623 | 1583 | } |
|
| 1584 | + | if ((u64)DATA_RO_OFFSET + rodata_bytes > DATA_MEMORY_START) { |
|
| 1585 | + | fclose(file); |
|
| 1586 | + | bail("read-only data section exceeds available program memory"); |
|
| 1587 | + | } |
|
| 1588 | + | program_base = align(DATA_RO_OFFSET + rodata_bytes, WORD_SIZE); |
|
| 1589 | + | if ((u64)program_base + program_bytes > DATA_MEMORY_START) { |
|
| 1590 | + | fclose(file); |
|
| 1591 | + | bail("text section exceeds available program memory"); |
|
| 1592 | + | } |
|
| 1593 | + | if ((u64)DATA_MEMORY_START + data_bytes > data_limit) { |
|
| 1594 | + | fclose(file); |
|
| 1595 | + | bail("read-write data section exceeds available data memory"); |
|
| 1596 | + | } |
|
| 1597 | + | ||
| 1598 | + | load_image_section(file, program_base, program_bytes, "text"); |
|
| 1599 | + | load_image_section(file, DATA_RO_OFFSET, rodata_bytes, "read-only data"); |
|
| 1600 | + | load_image_section(file, DATA_MEMORY_START, data_bytes, "read-write data"); |
|
| 1624 | 1601 | fclose(file); |
|
| 1625 | 1602 | ||
| 1626 | 1603 | cpu->programsize = program_bytes / sizeof(instr_t); |
|
| 1627 | 1604 | cpu->pc = program_base; |
|
| 1628 | 1605 | } |
riscv.c
+1 -258
| 1 | - | /* RISC-V 64-bit (RV64I) instruction builder. */ |
|
| 2 | - | #include <stdlib.h> |
|
| 3 | - | ||
| 1 | + | /* RISC-V register names and small decoding helpers. */ |
|
| 4 | 2 | #include "riscv.h" |
|
| 5 | 3 | #include "types.h" |
|
| 6 | 4 | ||
| 7 | 5 | const char *reg_names[] = { |
|
| 8 | 6 | "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", "fp", "s1", "a0", |
|
| 9 | 7 | "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4", "s5", |
|
| 10 | 8 | "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6" |
|
| 11 | 9 | }; |
|
| 12 | 10 | ||
| 13 | - | const bool caller_saved_registers[] = { |
|
| 14 | - | false, false, false, false, false, true, true, true, false, false, true, |
|
| 15 | - | true, true, true, true, true, true, true, false, false, false, false, |
|
| 16 | - | false, false, false, false, false, false, true, true, true, true |
|
| 17 | - | }; |
|
| 18 | - | ||
| 19 | - | const reg_t temp_registers[] = { T1, T2, T3, T4, T5, T6 }; |
|
| 20 | - | ||
| 21 | 11 | i32 sign_extend(u32 value, int bit_width) { |
|
| 22 | 12 | if ((value >> (bit_width - 1)) & 1) { |
|
| 23 | 13 | /* Sign bit is 1, so extend with 1s. */ |
|
| 24 | 14 | return (i32)(value | (~0u << bit_width)); |
|
| 25 | 15 | } |
| 29 | 19 | i32 align(i32 size, i32 alignment) { |
|
| 30 | 20 | /* Verify alignment is a power of 2. */ |
|
| 31 | 21 | /* This rounds up to the next multiple of alignment. */ |
|
| 32 | 22 | return (size + alignment - 1) & ~(alignment - 1); |
|
| 33 | 23 | } |
|
| 34 | - | ||
| 35 | - | /* Creates an I-type instruction struct. |
|
| 36 | - | * Used for immediate operations like ADDI, SLTI and loads like LW. */ |
|
| 37 | - | static instr_t instr_i( |
|
| 38 | - | opcode_t opcode, funct3_t fn3, reg_t rd, reg_t rs1, i32 imm |
|
| 39 | - | ) { |
|
| 40 | - | return (instr_t){ .i = { .opcode = opcode, |
|
| 41 | - | .rd = rd, |
|
| 42 | - | .rs1 = rs1, |
|
| 43 | - | .funct3 = fn3, |
|
| 44 | - | .imm_11_0 = (u32)imm } }; |
|
| 45 | - | } |
|
| 46 | - | ||
| 47 | - | /* Creates a U-type instruction struct. |
|
| 48 | - | * Used for LUI (Load Upper Immediate) and AUIPC. */ |
|
| 49 | - | static instr_t instr_u(opcode_t opcode, reg_t rd, i32 imm) { |
|
| 50 | - | return (instr_t){ .u = { |
|
| 51 | - | .opcode = opcode, |
|
| 52 | - | .rd = rd, |
|
| 53 | - | .imm_31_12 = (u32)imm & 0xFFFFF, |
|
| 54 | - | } }; |
|
| 55 | - | } |
|
| 56 | - | ||
| 57 | - | /* Creates an R-type instruction struct. |
|
| 58 | - | * Used for register-register operations like ADD, SUB, AND, OR. */ |
|
| 59 | - | static instr_t instr_r( |
|
| 60 | - | opcode_t opcode, funct3_t fn3, funct7_t fn7, reg_t rd, reg_t rs1, reg_t rs2 |
|
| 61 | - | ) { |
|
| 62 | - | return (instr_t){ .r = { .opcode = opcode, |
|
| 63 | - | .rd = rd, |
|
| 64 | - | .rs1 = rs1, |
|
| 65 | - | .rs2 = rs2, |
|
| 66 | - | .funct3 = fn3, |
|
| 67 | - | .funct7 = fn7 } }; |
|
| 68 | - | } |
|
| 69 | - | ||
| 70 | - | /* Creates an S-type instruction struct. |
|
| 71 | - | * Used for store instructions like SW, SH, SB. */ |
|
| 72 | - | static instr_t instr_s( |
|
| 73 | - | opcode_t opcode, funct3_t fn3, reg_t rs1, reg_t rs2, i32 imm |
|
| 74 | - | ) { |
|
| 75 | - | return (instr_t){ .s = { .opcode = opcode, |
|
| 76 | - | .rs1 = rs1, |
|
| 77 | - | .rs2 = rs2, |
|
| 78 | - | .funct3 = fn3, |
|
| 79 | - | .imm_4_0 = (u32)imm & 0x1F, |
|
| 80 | - | .imm_11_5 = ((u32)imm >> 5) & 0x7F } }; |
|
| 81 | - | } |
|
| 82 | - | ||
| 83 | - | /* Creates an SB-type (branch) instruction struct. |
|
| 84 | - | * Modified S-type used for conditional branches like BEQ, BNE. */ |
|
| 85 | - | static instr_t instr_sb( |
|
| 86 | - | opcode_t opcode, funct3_t fn3, reg_t rs1, reg_t rs2, i32 imm |
|
| 87 | - | ) { |
|
| 88 | - | return (instr_t){ .b = { .opcode = opcode, |
|
| 89 | - | .rs1 = rs1, |
|
| 90 | - | .rs2 = rs2, |
|
| 91 | - | .funct3 = fn3, |
|
| 92 | - | .imm_11 = (imm >> 11) & 0x1, |
|
| 93 | - | .imm_4_1 = (imm >> 1) & 0xF, |
|
| 94 | - | .imm_10_5 = (imm >> 5) & 0x3F, |
|
| 95 | - | .imm_12 = (imm >> 12) & 0x1 } }; |
|
| 96 | - | } |
|
| 97 | - | ||
| 98 | - | /* Creates a UJ-type (jump) instruction struct. |
|
| 99 | - | * Modified U-type used for JAL instruction. */ |
|
| 100 | - | static instr_t instr_uj(opcode_t opcode, reg_t rd, i32 imm) { |
|
| 101 | - | return (instr_t){ .j = { .opcode = opcode, |
|
| 102 | - | .rd = rd, |
|
| 103 | - | .imm_20 = (imm >> 20) & 0x1, |
|
| 104 | - | .imm_10_1 = (imm >> 1) & 0x3FF, |
|
| 105 | - | .imm_11 = (imm >> 11) & 0x1, |
|
| 106 | - | .imm_19_12 = (imm >> 12) & 0xFF } }; |
|
| 107 | - | } |
|
| 108 | - | ||
| 109 | - | /* Instruction definitions table. */ |
|
| 110 | - | /* Maps each instruction to its parameters for easy lookup. */ |
|
| 111 | - | typedef struct { |
|
| 112 | - | ifmt_t type; /* Instruction type */ |
|
| 113 | - | opcode_t opcode; /* Opcode value */ |
|
| 114 | - | funct3_t funct3; /* Function3 value (if applicable) */ |
|
| 115 | - | funct7_t funct7; /* Function7 value (if applicable) */ |
|
| 116 | - | bool special; /* Special handling flag */ |
|
| 117 | - | } idef_t; |
|
| 118 | - | ||
| 119 | - | /* Instruction definition table */ |
|
| 120 | - | static const idef_t idefs[] = { |
|
| 121 | - | /* Upper immediate instructions. */ |
|
| 122 | - | [I_LUI] = { IFMT_U, OP_LUI, 0, 0, true }, |
|
| 123 | - | [I_AUIPC] = { IFMT_U, OP_AUIPC, 0, 0, true }, |
|
| 124 | - | /* Jump instructions. */ |
|
| 125 | - | [I_JAL] = { IFMT_J, OP_JAL, 0, 0, true }, |
|
| 126 | - | [I_JALR] = { IFMT_I, OP_JALR, 0, 0, false }, |
|
| 127 | - | /* Branch instructions. */ |
|
| 128 | - | [I_BEQ] = { IFMT_B, OP_BRANCH, FUNCT3_BYTE, 0, false }, |
|
| 129 | - | [I_BNE] = { IFMT_B, OP_BRANCH, FUNCT3_HALF, 0, false }, |
|
| 130 | - | [I_BLT] = { IFMT_B, OP_BRANCH, FUNCT3_BYTE_U, 0, false }, |
|
| 131 | - | [I_BGE] = { IFMT_B, OP_BRANCH, FUNCT3_HALF_U, 0, false }, |
|
| 132 | - | [I_BLTU] = { IFMT_B, OP_BRANCH, FUNCT3_OR, 0, false }, |
|
| 133 | - | [I_BGEU] = { IFMT_B, OP_BRANCH, FUNCT3_AND, 0, false }, |
|
| 134 | - | /* Load instructions. */ |
|
| 135 | - | [I_LB] = { IFMT_I, OP_LOAD, FUNCT3_BYTE, 0, false }, |
|
| 136 | - | [I_LH] = { IFMT_I, OP_LOAD, FUNCT3_HALF, 0, false }, |
|
| 137 | - | [I_LW] = { IFMT_I, OP_LOAD, FUNCT3_WORD, 0, false }, |
|
| 138 | - | [I_LBU] = { IFMT_I, OP_LOAD, FUNCT3_BYTE_U, 0, false }, |
|
| 139 | - | [I_LHU] = { IFMT_I, OP_LOAD, FUNCT3_HALF_U, 0, false }, |
|
| 140 | - | /* Store instructions. */ |
|
| 141 | - | [I_SB] = { IFMT_S, OP_STORE, FUNCT3_BYTE, 0, false }, |
|
| 142 | - | [I_SH] = { IFMT_S, OP_STORE, FUNCT3_HALF, 0, false }, |
|
| 143 | - | [I_SW] = { IFMT_S, OP_STORE, FUNCT3_WORD, 0, false }, |
|
| 144 | - | /* ALU immediate operations. */ |
|
| 145 | - | [I_ADDI] = { IFMT_I, OP_IMM, FUNCT3_ADD, 0, false }, |
|
| 146 | - | [I_SLTI] = { IFMT_I, OP_IMM, FUNCT3_SLT, 0, false }, |
|
| 147 | - | [I_SLTIU] = { IFMT_I, OP_IMM, FUNCT3_SLTU, 0, false }, |
|
| 148 | - | [I_XORI] = { IFMT_I, OP_IMM, FUNCT3_XOR, 0, false }, |
|
| 149 | - | [I_ORI] = { IFMT_I, OP_IMM, FUNCT3_OR, 0, false }, |
|
| 150 | - | [I_ANDI] = { IFMT_I, OP_IMM, FUNCT3_AND, 0, false }, |
|
| 151 | - | [I_SLLI] = { IFMT_I, OP_IMM, FUNCT3_SLL, 0, true }, |
|
| 152 | - | [I_SRLI] = { IFMT_I, OP_IMM, FUNCT3_SRL, 0, true }, |
|
| 153 | - | [I_SRAI] = { IFMT_I, OP_IMM, FUNCT3_SRL, 0, true }, |
|
| 154 | - | /* ALU register operations. */ |
|
| 155 | - | [I_ADD] = { IFMT_R, OP_OP, FUNCT3_ADD, FUNCT7_NORMAL, false }, |
|
| 156 | - | [I_SUB] = { IFMT_R, OP_OP, FUNCT3_ADD, FUNCT7_SUB, false }, |
|
| 157 | - | [I_SLL] = { IFMT_R, OP_OP, FUNCT3_SLL, FUNCT7_NORMAL, false }, |
|
| 158 | - | [I_SLT] = { IFMT_R, OP_OP, FUNCT3_SLT, FUNCT7_NORMAL, false }, |
|
| 159 | - | [I_SLTU] = { IFMT_R, OP_OP, FUNCT3_SLTU, FUNCT7_NORMAL, false }, |
|
| 160 | - | [I_XOR] = { IFMT_R, OP_OP, FUNCT3_XOR, FUNCT7_NORMAL, false }, |
|
| 161 | - | [I_SRL] = { IFMT_R, OP_OP, FUNCT3_SRL, FUNCT7_NORMAL, false }, |
|
| 162 | - | [I_AND] = { IFMT_R, OP_OP, FUNCT3_AND, FUNCT7_NORMAL, false }, |
|
| 163 | - | [I_OR] = { IFMT_R, OP_OP, FUNCT3_OR, FUNCT7_NORMAL, false }, |
|
| 164 | - | /* M extension - multiply and divide. */ |
|
| 165 | - | [I_MUL] = { IFMT_R, OP_OP, FUNCT3_ADD, FUNCT7_MUL, false }, |
|
| 166 | - | [I_MULH] = { IFMT_R, OP_OP, FUNCT3_SLL, FUNCT7_MUL, false }, |
|
| 167 | - | [I_MULHSU] = { IFMT_R, OP_OP, FUNCT3_SLT, FUNCT7_MUL, false }, |
|
| 168 | - | [I_MULHU] = { IFMT_R, OP_OP, FUNCT3_SLTU, FUNCT7_MUL, false }, |
|
| 169 | - | [I_DIV] = { IFMT_R, OP_OP, FUNCT3_XOR, FUNCT7_MUL, false }, |
|
| 170 | - | [I_DIVU] = { IFMT_R, OP_OP, FUNCT3_SRL, FUNCT7_MUL, false }, |
|
| 171 | - | [I_REM] = { IFMT_R, OP_OP, FUNCT3_OR, FUNCT7_MUL, false }, |
|
| 172 | - | [I_REMU] = { IFMT_R, OP_OP, FUNCT3_AND, FUNCT7_MUL, false }, |
|
| 173 | - | /* Pseudo-instructions. */ |
|
| 174 | - | [I_MV] = { IFMT_I, OP_IMM, FUNCT3_ADD, 0, true }, |
|
| 175 | - | [I_JMP] = { IFMT_J, OP_JAL, 0, 0, true }, |
|
| 176 | - | [I_NOP] = { IFMT_I, OP_IMM, FUNCT3_ADD, 0, true }, |
|
| 177 | - | [I_NOT] = { IFMT_I, OP_IMM, FUNCT3_XOR, 0, true }, |
|
| 178 | - | [I_NEG] = { IFMT_R, OP_OP, FUNCT3_ADD, FUNCT7_SUB, true }, |
|
| 179 | - | /* System instructions */ |
|
| 180 | - | [I_EBREAK] = { IFMT_I, OP_SYSTEM, 0, 0, true }, |
|
| 181 | - | [I_ECALL] = { IFMT_I, OP_SYSTEM, 0, 0, true }, |
|
| 182 | - | /* F Extension floating-point instructions */ |
|
| 183 | - | [I_FADD_S] = { IFMT_R, OP_OP_FP, FUNCT3_ADD, FUNCT7_FADD_S, false }, |
|
| 184 | - | [I_FSUB_S] = { IFMT_R, OP_OP_FP, FUNCT3_ADD, FUNCT7_FSUB_S, false }, |
|
| 185 | - | [I_FMUL_S] = { IFMT_R, OP_OP_FP, FUNCT3_ADD, FUNCT7_FMUL_S, false }, |
|
| 186 | - | [I_FDIV_S] = { IFMT_R, OP_OP_FP, FUNCT3_ADD, FUNCT7_FDIV_S, false }, |
|
| 187 | - | [I_FEQ_S] = { IFMT_R, OP_OP_FP, FUNCT3_FEQ, FUNCT7_FEQ_S, false }, |
|
| 188 | - | [I_FLT_S] = { IFMT_R, OP_OP_FP, FUNCT3_FLT, FUNCT7_FLT_S, false }, |
|
| 189 | - | [I_FLE_S] = { IFMT_R, OP_OP_FP, FUNCT3_FLE, FUNCT7_FLE_S, false }, |
|
| 190 | - | [I_FLW] = { IFMT_I, OP_LOAD_FP, FUNCT3_WORD_FP, 0, false }, |
|
| 191 | - | [I_FSW] = { IFMT_S, OP_STORE_FP, FUNCT3_WORD_FP, 0, false }, |
|
| 192 | - | /* RV64I load/store */ |
|
| 193 | - | [I_LWU] = { IFMT_I, OP_LOAD, FUNCT3_WORD_U, 0, false }, |
|
| 194 | - | [I_LD] = { IFMT_I, OP_LOAD, FUNCT3_DOUBLE, 0, false }, |
|
| 195 | - | [I_SD] = { IFMT_S, OP_STORE, FUNCT3_DOUBLE, 0, false }, |
|
| 196 | - | /* RV64I immediate W-ops */ |
|
| 197 | - | [I_ADDIW] = { IFMT_I, OP_IMM_32, FUNCT3_ADD, 0, false }, |
|
| 198 | - | [I_SLLIW] = { IFMT_I, OP_IMM_32, FUNCT3_SLL, 0, true }, |
|
| 199 | - | [I_SRLIW] = { IFMT_I, OP_IMM_32, FUNCT3_SRL, 0, true }, |
|
| 200 | - | [I_SRAIW] = { IFMT_I, OP_IMM_32, FUNCT3_SRL, 0, true }, |
|
| 201 | - | /* RV64I register W-ops */ |
|
| 202 | - | [I_ADDW] = { IFMT_R, OP_OP_32, FUNCT3_ADD, FUNCT7_NORMAL, false }, |
|
| 203 | - | [I_SUBW] = { IFMT_R, OP_OP_32, FUNCT3_ADD, FUNCT7_SUB, false }, |
|
| 204 | - | [I_SLLW] = { IFMT_R, OP_OP_32, FUNCT3_SLL, FUNCT7_NORMAL, false }, |
|
| 205 | - | [I_SRLW] = { IFMT_R, OP_OP_32, FUNCT3_SRL, FUNCT7_NORMAL, false }, |
|
| 206 | - | [I_SRAW] = { IFMT_R, OP_OP_32, FUNCT3_SRL, FUNCT7_SRA, false }, |
|
| 207 | - | /* RV64M W-ops */ |
|
| 208 | - | [I_MULW] = { IFMT_R, OP_OP_32, FUNCT3_ADD, FUNCT7_MUL, false }, |
|
| 209 | - | [I_DIVW] = { IFMT_R, OP_OP_32, FUNCT3_XOR, FUNCT7_MUL, false }, |
|
| 210 | - | [I_DIVUW] = { IFMT_R, OP_OP_32, FUNCT3_SRL, FUNCT7_MUL, false }, |
|
| 211 | - | [I_REMW] = { IFMT_R, OP_OP_32, FUNCT3_OR, FUNCT7_MUL, false }, |
|
| 212 | - | [I_REMUW] = { IFMT_R, OP_OP_32, FUNCT3_AND, FUNCT7_MUL, false }, |
|
| 213 | - | }; |
|
| 214 | - | ||
| 215 | - | /* Generates a RISC-V instruction based on the instruction definition */ |
|
| 216 | - | instr_t instr(iname_t iop, reg_t rd, reg_t rs1, reg_t rs2, i32 imm) { |
|
| 217 | - | const idef_t *def = &idefs[iop]; |
|
| 218 | - | ||
| 219 | - | /* Handle special cases that need specific processing. */ |
|
| 220 | - | if (def->special) { |
|
| 221 | - | switch (iop) { |
|
| 222 | - | case I_LUI: |
|
| 223 | - | return instr_u(OP_LUI, rd, imm); |
|
| 224 | - | case I_AUIPC: |
|
| 225 | - | return instr_u(OP_AUIPC, rd, imm); |
|
| 226 | - | case I_JAL: |
|
| 227 | - | return instr_uj(OP_JAL, rd, imm); |
|
| 228 | - | case I_SLLI: |
|
| 229 | - | return instr_i(OP_IMM, FUNCT3_SLL, rd, rs1, imm & 0x3F); |
|
| 230 | - | case I_SRLI: |
|
| 231 | - | return instr_i(OP_IMM, FUNCT3_SRL, rd, rs1, imm & 0x3F); |
|
| 232 | - | case I_SRAI: |
|
| 233 | - | return instr_i(OP_IMM, FUNCT3_SRL, rd, rs1, (imm & 0x3F) + 0x400); |
|
| 234 | - | case I_SLLIW: |
|
| 235 | - | return instr_i(OP_IMM_32, FUNCT3_SLL, rd, rs1, imm & 0x1F); |
|
| 236 | - | case I_SRLIW: |
|
| 237 | - | return instr_i(OP_IMM_32, FUNCT3_SRL, rd, rs1, imm & 0x1F); |
|
| 238 | - | case I_SRAIW: |
|
| 239 | - | return instr_i( |
|
| 240 | - | OP_IMM_32, FUNCT3_SRL, rd, rs1, (imm & 0x1F) + 0x400 |
|
| 241 | - | ); |
|
| 242 | - | case I_MV: |
|
| 243 | - | return instr_i(OP_IMM, FUNCT3_ADD, rd, rs1, 0); |
|
| 244 | - | case I_JMP: |
|
| 245 | - | return instr_uj(OP_JAL, ZERO, imm); |
|
| 246 | - | case I_NOP: |
|
| 247 | - | return instr_i(OP_IMM, FUNCT3_ADD, ZERO, ZERO, 0); |
|
| 248 | - | case I_NOT: |
|
| 249 | - | return instr_i(OP_IMM, FUNCT3_XOR, rd, rs1, 1); |
|
| 250 | - | case I_NEG: |
|
| 251 | - | return instr_r(OP_OP, FUNCT3_ADD, FUNCT7_SUB, rd, ZERO, rs1); |
|
| 252 | - | case I_EBREAK: |
|
| 253 | - | /* EBREAK is encoded as all zeros except for the opcode */ |
|
| 254 | - | return instr_i(OP_SYSTEM, 0, 0, 0, 1); |
|
| 255 | - | case I_ECALL: |
|
| 256 | - | /* ECALL is encoded as all zeros including immediate */ |
|
| 257 | - | return instr_i(OP_SYSTEM, 0, 0, 0, 0); |
|
| 258 | - | default: |
|
| 259 | - | break; |
|
| 260 | - | } |
|
| 261 | - | } |
|
| 262 | - | ||
| 263 | - | /* Regular instructions by type. */ |
|
| 264 | - | switch (def->type) { |
|
| 265 | - | case IFMT_I: |
|
| 266 | - | return instr_i(def->opcode, def->funct3, rd, rs1, imm); |
|
| 267 | - | case IFMT_R: |
|
| 268 | - | return instr_r(def->opcode, def->funct3, def->funct7, rd, rs1, rs2); |
|
| 269 | - | case IFMT_S: |
|
| 270 | - | return instr_s(def->opcode, def->funct3, rs1, rs2, imm); |
|
| 271 | - | case IFMT_B: |
|
| 272 | - | return instr_sb(def->opcode, def->funct3, rs1, rs2, imm); |
|
| 273 | - | case IFMT_U: |
|
| 274 | - | return instr_u(def->opcode, rd, imm); |
|
| 275 | - | case IFMT_J: |
|
| 276 | - | return instr_uj(def->opcode, rd, imm); |
|
| 277 | - | default: |
|
| 278 | - | abort(); |
|
| 279 | - | } |
|
| 280 | - | } |
riscv.h
+7 -236
| 2 | 2 | #define OP_H |
|
| 3 | 3 | ||
| 4 | 4 | #include "types.h" |
|
| 5 | 5 | ||
| 6 | 6 | /* Total number of registers. */ |
|
| 7 | - | #define REGISTERS 32 |
|
| 7 | + | #define REGISTERS 32 |
|
| 8 | 8 | /* Word size of target architecture (RISCV64). */ |
|
| 9 | - | #define WORD_SIZE 8 |
|
| 10 | - | /* Tag size for optional/union discriminants. */ |
|
| 11 | - | #define TAG_SIZE 1 |
|
| 9 | + | #define WORD_SIZE 8 |
|
| 12 | 10 | /* Instruction size in bytes (always 32-bit, even on RV64). */ |
|
| 13 | - | #define INSTR_SIZE 4 |
|
| 14 | - | /* Stack alignment requirement. */ |
|
| 15 | - | #define STACK_ALIGNMENT 16 |
|
| 11 | + | #define INSTR_SIZE 4 |
|
| 16 | 12 | /* The frame pointer register is set as an alias of `S0`. */ |
|
| 17 | - | #define FP S0 |
|
| 18 | - | ||
| 19 | - | /* Convenient macro wrappers for `instr`. |
|
| 20 | - | * Some of these, such as BLE and BGT are implemented by swapping the operands |
|
| 21 | - | * of other instructions. */ |
|
| 22 | - | #define ADDI(rd, rs1, imm) __instr(I_ADDI, rd, rs1, 0, imm) |
|
| 23 | - | #define SLTI(rd, rs1, imm) __instr(I_SLTI, rd, rs1, 0, imm) |
|
| 24 | - | #define SLTIU(rd, rs1, imm) __instr(I_SLTIU, rd, rs1, 0, imm) |
|
| 25 | - | #define XORI(rd, rs1, imm) __instr(I_XORI, rd, rs1, 0, imm) |
|
| 26 | - | #define ORI(rd, rs1, imm) __instr(I_ORI, rd, rs1, 0, imm) |
|
| 27 | - | #define ANDI(rd, rs1, imm) __instr(I_ANDI, rd, rs1, 0, imm) |
|
| 28 | - | #define SLLI(rd, rs1, imm) __instr(I_SLLI, rd, rs1, 0, imm) |
|
| 29 | - | #define SRLI(rd, rs1, imm) __instr(I_SRLI, rd, rs1, 0, imm) |
|
| 30 | - | #define SRAI(rd, rs1, imm) __instr(I_SRAI, rd, rs1, 0, imm) |
|
| 31 | - | #define JALR(rd, rs1, imm) __instr(I_JALR, rd, rs1, 0, imm) |
|
| 32 | - | #define LB(rd, rs1, imm) __instr(I_LB, rd, rs1, 0, imm) |
|
| 33 | - | #define LH(rd, rs1, imm) __instr(I_LH, rd, rs1, 0, imm) |
|
| 34 | - | #define LW(rd, rs1, imm) __instr(I_LW, rd, rs1, 0, imm) |
|
| 35 | - | #define LWU(rd, rs1, imm) __instr(I_LWU, rd, rs1, 0, imm) |
|
| 36 | - | #define LD(rd, rs1, imm) __instr(I_LD, rd, rs1, 0, imm) |
|
| 37 | - | #define LBU(rd, rs1, imm) __instr(I_LBU, rd, rs1, 0, imm) |
|
| 38 | - | #define LHU(rd, rs1, imm) __instr(I_LHU, rd, rs1, 0, imm) |
|
| 39 | - | #define SB(rs2, rs1, imm) __instr(I_SB, 0, rs1, rs2, imm) |
|
| 40 | - | #define SH(rs2, rs1, imm) __instr(I_SH, 0, rs1, rs2, imm) |
|
| 41 | - | #define SW(rs2, rs1, imm) __instr(I_SW, 0, rs1, rs2, imm) |
|
| 42 | - | #define SD(rs2, rs1, imm) __instr(I_SD, 0, rs1, rs2, imm) |
|
| 43 | - | #define BEQ(rs1, rs2, imm) __instr(I_BEQ, 0, rs1, rs2, imm) |
|
| 44 | - | #define BNE(rs1, rs2, imm) __instr(I_BNE, 0, rs1, rs2, imm) |
|
| 45 | - | #define BLT(rs1, rs2, imm) __instr(I_BLT, 0, rs1, rs2, imm) |
|
| 46 | - | #define BGE(rs1, rs2, imm) __instr(I_BGE, 0, rs1, rs2, imm) |
|
| 47 | - | #define BLTU(rs1, rs2, imm) __instr(I_BLTU, 0, rs1, rs2, imm) |
|
| 48 | - | #define BGEU(rs1, rs2, imm) __instr(I_BGEU, 0, rs1, rs2, imm) |
|
| 49 | - | #define BLE(rs1, rs2, imm) __instr(I_BGE, 0, rs2, rs1, imm) |
|
| 50 | - | #define BGT(rs1, rs2, imm) __instr(I_BLT, 0, rs2, rs1, imm) |
|
| 51 | - | #define ADD(rd, rs1, rs2) __instr(I_ADD, rd, rs1, rs2, 0) |
|
| 52 | - | #define SUB(rd, rs1, rs2) __instr(I_SUB, rd, rs1, rs2, 0) |
|
| 53 | - | #define DIV(rd, rs1, rs2) __instr(I_DIV, rd, rs1, rs2, 0) |
|
| 54 | - | #define DIVU(rd, rs1, rs2) __instr(I_DIVU, rd, rs1, rs2, 0) |
|
| 55 | - | #define REM(rd, rs1, rs2) __instr(I_REM, rd, rs1, rs2, 0) |
|
| 56 | - | #define REMU(rd, rs1, rs2) __instr(I_REMU, rd, rs1, rs2, 0) |
|
| 57 | - | #define MUL(rd, rs1, rs2) __instr(I_MUL, rd, rs1, rs2, 0) |
|
| 58 | - | #define SLL(rd, rs1, rs2) __instr(I_SLL, rd, rs1, rs2, 0) |
|
| 59 | - | #define SLT(rd, rs1, rs2) __instr(I_SLT, rd, rs1, rs2, 0) |
|
| 60 | - | #define SLTU(rd, rs1, rs2) __instr(I_SLTU, rd, rs1, rs2, 0) |
|
| 61 | - | #define XOR(rd, rs1, rs2) __instr(I_XOR, rd, rs1, rs2, 0) |
|
| 62 | - | #define SRL(rd, rs1, rs2) __instr(I_SRL, rd, rs1, rs2, 0) |
|
| 63 | - | #define AND(rd, rs1, rs2) __instr(I_AND, rd, rs1, rs2, 0) |
|
| 64 | - | #define OR(rd, rs1, rs2) __instr(I_OR, rd, rs1, rs2, 0) |
|
| 65 | - | #define LUI(rd, imm) __instr(I_LUI, rd, 0, 0, imm) |
|
| 66 | - | #define AUIPC(rd, imm) __instr(I_AUIPC, rd, 0, 0, imm) |
|
| 67 | - | #define JAL(rd, imm) __instr(I_JAL, rd, 0, 0, imm) |
|
| 68 | - | #define JMP(imm) __instr(I_JMP, 0, 0, 0, imm) |
|
| 69 | - | #define MV(rd, rs1) __instr(I_MV, rd, rs1, 0, 0) |
|
| 70 | - | #define NOT(rd, rs1) __instr(I_NOT, rd, rs1, 0, 0) |
|
| 71 | - | #define NEG(rd, rs1) __instr(I_NEG, rd, rs1, 0, 0) |
|
| 72 | - | #define NOP __instr(I_NOP, 0, 0, 0, 0) |
|
| 73 | - | #define RET __instr(I_JALR, ZERO, RA, 0, 0) |
|
| 74 | - | #define EBREAK __instr(I_EBREAK, 0, 0, 0, 0) |
|
| 75 | - | #define ECALL __instr(I_ECALL, 0, 0, 0, 0) |
|
| 76 | - | /* RV64I word-width (32-bit) operations */ |
|
| 77 | - | #define ADDIW(rd, rs1, imm) __instr(I_ADDIW, rd, rs1, 0, imm) |
|
| 78 | - | #define ADDW(rd, rs1, rs2) __instr(I_ADDW, rd, rs1, rs2, 0) |
|
| 79 | - | #define SUBW(rd, rs1, rs2) __instr(I_SUBW, rd, rs1, rs2, 0) |
|
| 80 | - | #define MULW(rd, rs1, rs2) __instr(I_MULW, rd, rs1, rs2, 0) |
|
| 81 | - | #define DIVW(rd, rs1, rs2) __instr(I_DIVW, rd, rs1, rs2, 0) |
|
| 82 | - | #define DIVUW(rd, rs1, rs2) __instr(I_DIVUW, rd, rs1, rs2, 0) |
|
| 83 | - | #define REMW(rd, rs1, rs2) __instr(I_REMW, rd, rs1, rs2, 0) |
|
| 84 | - | #define REMUW(rd, rs1, rs2) __instr(I_REMUW, rd, rs1, rs2, 0) |
|
| 85 | - | #define SLLIW(rd, rs1, imm) __instr(I_SLLIW, rd, rs1, 0, imm) |
|
| 86 | - | #define SRLIW(rd, rs1, imm) __instr(I_SRLIW, rd, rs1, 0, imm) |
|
| 87 | - | #define SRAIW(rd, rs1, imm) __instr(I_SRAIW, rd, rs1, 0, imm) |
|
| 88 | - | #define SLLW(rd, rs1, rs2) __instr(I_SLLW, rd, rs1, rs2, 0) |
|
| 89 | - | #define SRLW(rd, rs1, rs2) __instr(I_SRLW, rd, rs1, rs2, 0) |
|
| 90 | - | #define SRAW(rd, rs1, rs2) __instr(I_SRAW, rd, rs1, rs2, 0) |
|
| 91 | - | /* F Extension - Floating-point instructions */ |
|
| 92 | - | #define FADD_S(rd, rs1, rs2) __instr(I_FADD_S, rd, rs1, rs2, 0) |
|
| 93 | - | #define FSUB_S(rd, rs1, rs2) __instr(I_FSUB_S, rd, rs1, rs2, 0) |
|
| 94 | - | #define FMUL_S(rd, rs1, rs2) __instr(I_FMUL_S, rd, rs1, rs2, 0) |
|
| 95 | - | #define FDIV_S(rd, rs1, rs2) __instr(I_FDIV_S, rd, rs1, rs2, 0) |
|
| 96 | - | #define FEQ_S(rd, rs1, rs2) __instr(I_FEQ_S, rd, rs1, rs2, 0) |
|
| 97 | - | #define FLT_S(rd, rs1, rs2) __instr(I_FLT_S, rd, rs1, rs2, 0) |
|
| 98 | - | #define FLE_S(rd, rs1, rs2) __instr(I_FLE_S, rd, rs1, rs2, 0) |
|
| 99 | - | #define FLW(rd, rs1, imm) __instr(I_FLW, rd, rs1, 0, imm) |
|
| 100 | - | #define FSW(rs2, rs1, imm) __instr(I_FSW, 0, rs1, rs2, imm) |
|
| 13 | + | #define FP S0 |
|
| 101 | 14 | ||
| 102 | 15 | /* String representations of register names. */ |
|
| 103 | 16 | extern const char *reg_names[]; |
|
| 104 | 17 | ||
| 105 | - | /* Boolean map of caller-saved registers. |
|
| 106 | - | * True for registers that need to be saved by the caller |
|
| 107 | - | * before a function call. */ |
|
| 108 | - | extern const bool caller_saved_registers[REGISTERS]; |
|
| 109 | - | ||
| 110 | 18 | /* RISC-V register names. */ |
|
| 111 | 19 | typedef enum { |
|
| 112 | 20 | ZERO = 0, /* Hard-wired zero */ |
|
| 113 | 21 | RA = 1, /* Return address */ |
|
| 114 | 22 | SP = 2, /* Stack pointer */ |
| 141 | 49 | T4 = 29, |
|
| 142 | 50 | T5 = 30, |
|
| 143 | 51 | T6 = 31 |
|
| 144 | 52 | } reg_t; |
|
| 145 | 53 | ||
| 146 | - | /* Temporary registers (T1-T6) */ |
|
| 147 | - | extern const reg_t temp_registers[6]; |
|
| 148 | - | ||
| 149 | 54 | /* Opcodes for RISC-V base instruction set */ |
|
| 150 | 55 | typedef enum { |
|
| 151 | 56 | OP_LOAD = 0x03, |
|
| 152 | 57 | OP_STORE = 0x23, |
|
| 153 | 58 | OP_BRANCH = 0x63, |
| 158 | 63 | OP_AUIPC = 0x17, |
|
| 159 | 64 | OP_IMM_32 = 0x1B, /* RV64I: ADDIW, SLLIW, SRLIW, SRAIW */ |
|
| 160 | 65 | OP_OP_32 = 0x3B, /* RV64I: ADDW, SUBW, SLLW, SRLW, SRAW, MULW, DIVW, REMW */ |
|
| 161 | 66 | OP_LUI = 0x37, |
|
| 162 | 67 | OP_SYSTEM = 0x73, |
|
| 163 | - | OP_FENCE = 0x0F, |
|
| 164 | - | /* F Extension opcodes */ |
|
| 165 | - | OP_LOAD_FP = 0x07, |
|
| 166 | - | OP_STORE_FP = 0x27, |
|
| 167 | - | OP_OP_FP = 0x53 |
|
| 68 | + | OP_FENCE = 0x0F |
|
| 168 | 69 | } opcode_t; |
|
| 169 | 70 | ||
| 170 | 71 | /* Function3 values */ |
|
| 171 | 72 | typedef enum { |
|
| 172 | 73 | /* Memory operations */ |
| 184 | 85 | FUNCT3_SLT = 0x2, /* SLT/SLTI */ |
|
| 185 | 86 | FUNCT3_SLTU = 0x3, /* SLTU/SLTIU */ |
|
| 186 | 87 | FUNCT3_XOR = 0x4, /* XOR/XORI */ |
|
| 187 | 88 | FUNCT3_SRL = 0x5, /* SRL/SRA/SRLI/SRAI */ |
|
| 188 | 89 | FUNCT3_OR = 0x6, /* OR/ORI */ |
|
| 189 | - | FUNCT3_AND = 0x7, /* AND/ANDI */ |
|
| 190 | - | /* F Extension function3 codes */ |
|
| 191 | - | FUNCT3_WORD_FP = 0x2, /* FLW/FSW - Load/Store Single */ |
|
| 192 | - | FUNCT3_FEQ = 0x2, /* FEQ.S */ |
|
| 193 | - | FUNCT3_FLT = 0x1, /* FLT.S */ |
|
| 194 | - | FUNCT3_FLE = 0x0 /* FLE.S */ |
|
| 90 | + | FUNCT3_AND = 0x7 /* AND/ANDI */ |
|
| 195 | 91 | } funct3_t; |
|
| 196 | 92 | ||
| 197 | 93 | /* Function7 values */ |
|
| 198 | 94 | typedef enum { |
|
| 199 | 95 | FUNCT7_NORMAL = 0x00, |
|
| 200 | 96 | FUNCT7_SUB = 0x20, |
|
| 201 | - | FUNCT7_SRA = 0x20, |
|
| 202 | - | FUNCT7_MUL = 0x01, |
|
| 203 | - | /* F Extension function codes */ |
|
| 204 | - | FUNCT7_FADD_S = 0x00, |
|
| 205 | - | FUNCT7_FSUB_S = 0x04, |
|
| 206 | - | FUNCT7_FMUL_S = 0x08, |
|
| 207 | - | FUNCT7_FDIV_S = 0x0C, |
|
| 208 | - | FUNCT7_FEQ_S = 0x50, |
|
| 209 | - | FUNCT7_FLT_S = 0x50, |
|
| 210 | - | FUNCT7_FLE_S = 0x50 |
|
| 97 | + | FUNCT7_MUL = 0x01 |
|
| 211 | 98 | } funct7_t; |
|
| 212 | 99 | ||
| 213 | 100 | /* Represents a RISC-V instruction in its various formats */ |
|
| 214 | 101 | typedef union { |
|
| 215 | 102 | struct { |
| 265 | 152 | } j; /* Jump format */ |
|
| 266 | 153 | ||
| 267 | 154 | u32 raw; /* Raw 32-bit instruction */ |
|
| 268 | 155 | } instr_t; |
|
| 269 | 156 | ||
| 270 | - | /* Instruction type. */ |
|
| 271 | - | typedef enum { |
|
| 272 | - | IFMT_I, /* I-type (immediate) */ |
|
| 273 | - | IFMT_R, /* R-type (register) */ |
|
| 274 | - | IFMT_S, /* S-type (store) */ |
|
| 275 | - | IFMT_B, /* B-type (branch) */ |
|
| 276 | - | IFMT_U, /* U-type (upper immediate) */ |
|
| 277 | - | IFMT_J, /* J-type (jump) */ |
|
| 278 | - | } ifmt_t; |
|
| 279 | - | ||
| 280 | - | /* RISC-V instruction name. */ |
|
| 281 | - | typedef enum { |
|
| 282 | - | I_LUI, |
|
| 283 | - | I_AUIPC, |
|
| 284 | - | I_JAL, |
|
| 285 | - | I_JALR, |
|
| 286 | - | I_BEQ, |
|
| 287 | - | I_BNE, |
|
| 288 | - | I_BLT, |
|
| 289 | - | I_BGE, |
|
| 290 | - | I_BLTU, |
|
| 291 | - | I_BGEU, |
|
| 292 | - | I_LB, |
|
| 293 | - | I_LH, |
|
| 294 | - | I_LW, |
|
| 295 | - | I_LBU, |
|
| 296 | - | I_LHU, |
|
| 297 | - | I_SB, |
|
| 298 | - | I_SH, |
|
| 299 | - | I_SW, |
|
| 300 | - | I_ADDI, |
|
| 301 | - | I_SLTI, |
|
| 302 | - | I_SLTIU, |
|
| 303 | - | I_XORI, |
|
| 304 | - | I_ORI, |
|
| 305 | - | I_ANDI, |
|
| 306 | - | I_SLLI, |
|
| 307 | - | I_SRLI, |
|
| 308 | - | I_SRAI, |
|
| 309 | - | I_ADD, |
|
| 310 | - | I_SUB, |
|
| 311 | - | I_SLL, |
|
| 312 | - | I_SLT, |
|
| 313 | - | I_SLTU, |
|
| 314 | - | I_XOR, |
|
| 315 | - | I_SRL, |
|
| 316 | - | I_SRA, |
|
| 317 | - | I_OR, |
|
| 318 | - | I_AND, |
|
| 319 | - | I_MUL, |
|
| 320 | - | I_MULH, |
|
| 321 | - | I_MULHSU, |
|
| 322 | - | I_MULHU, |
|
| 323 | - | I_DIV, |
|
| 324 | - | I_DIVU, |
|
| 325 | - | I_REM, |
|
| 326 | - | I_REMU, |
|
| 327 | - | I_MV, |
|
| 328 | - | I_JMP, |
|
| 329 | - | I_NOP, |
|
| 330 | - | I_NOT, |
|
| 331 | - | I_NEG, |
|
| 332 | - | I_EBREAK, |
|
| 333 | - | I_ECALL, |
|
| 334 | - | /* F Extension - Floating-point instructions */ |
|
| 335 | - | I_FADD_S, |
|
| 336 | - | I_FSUB_S, |
|
| 337 | - | I_FMUL_S, |
|
| 338 | - | I_FDIV_S, |
|
| 339 | - | I_FEQ_S, |
|
| 340 | - | I_FLT_S, |
|
| 341 | - | I_FLE_S, |
|
| 342 | - | I_FLW, |
|
| 343 | - | I_FSW, |
|
| 344 | - | /* RV64I extensions */ |
|
| 345 | - | I_LWU, |
|
| 346 | - | I_LD, |
|
| 347 | - | I_SD, |
|
| 348 | - | I_ADDIW, |
|
| 349 | - | I_SLLIW, |
|
| 350 | - | I_SRLIW, |
|
| 351 | - | I_SRAIW, |
|
| 352 | - | I_ADDW, |
|
| 353 | - | I_SUBW, |
|
| 354 | - | I_SLLW, |
|
| 355 | - | I_SRLW, |
|
| 356 | - | I_SRAW, |
|
| 357 | - | I_MULW, |
|
| 358 | - | I_DIVW, |
|
| 359 | - | I_DIVUW, |
|
| 360 | - | I_REMW, |
|
| 361 | - | I_REMUW |
|
| 362 | - | } iname_t; |
|
| 363 | - | ||
| 364 | - | /* Returns a RISC-V instruction based on the instruction type. */ |
|
| 365 | - | instr_t instr(iname_t op, reg_t rd, reg_t rs1, reg_t rs2, i32 imm); |
|
| 366 | - | ||
| 367 | - | static inline instr_t __instr( |
|
| 368 | - | iname_t op, reg_t rd, reg_t rs1, reg_t rs2, i32 imm |
|
| 369 | - | ) { |
|
| 370 | - | return instr(op, rd, rs1, rs2, imm); |
|
| 371 | - | } |
|
| 372 | - | ||
| 373 | - | /* Return true when a signed 12-bit immediate can encode `value`. */ |
|
| 374 | - | static inline bool is_small(i32 value) { |
|
| 375 | - | return value >= -2048 && value <= 2047; |
|
| 376 | - | } |
|
| 377 | - | ||
| 378 | - | static inline bool is_branch_imm(i32 value) { |
|
| 379 | - | return value >= -(1 << 12) && value <= ((1 << 12) - 2) && !(value & 1); |
|
| 380 | - | } |
|
| 381 | - | ||
| 382 | - | static inline bool is_jump_imm(i32 value) { |
|
| 383 | - | return value >= -(1 << 20) && value <= ((1 << 20) - 2) && !(value & 1); |
|
| 384 | - | } |
|
| 385 | - | ||
| 386 | 157 | /* Helper function to sign-extend a value. */ |
|
| 387 | 158 | i32 sign_extend(u32 value, int bit_width); |
|
| 388 | 159 | /* Aligns a size to the specified alignment boundary. */ |
|
| 389 | 160 | i32 align(i32 size, i32 alignment); |
|
| 390 | 161 | /* Functions to get immediates out of instruction. */ |
riscv/debug.c
+0 -77
| 433 | 433 | return comp; |
|
| 434 | 434 | } |
|
| 435 | 435 | /* Fall through for unknown system instructions */ |
|
| 436 | 436 | ||
| 437 | 437 | /* fall through */ |
|
| 438 | - | case OP_LOAD_FP: |
|
| 439 | - | /* F Extension - Floating-point load instruction */ |
|
| 440 | - | comp = parts('I'); |
|
| 441 | - | imm = get_i_imm(instr); |
|
| 442 | - | ||
| 443 | - | if (instr.i.funct3 == FUNCT3_WORD_FP) { |
|
| 444 | - | strncpy(comp.op, "flw", MAX_PART_LEN); |
|
| 445 | - | } else { |
|
| 446 | - | strncpy( |
|
| 447 | - | comp.op, "flw", MAX_PART_LEN |
|
| 448 | - | ); /* Only single precision supported */ |
|
| 449 | - | } |
|
| 450 | - | strncpy(comp.rd, reg_names[instr.i.rd], MAX_PART_LEN); |
|
| 451 | - | snprintf(comp.off, MAX_PART_LEN, "%d", imm); |
|
| 452 | - | strncpy(comp.base, reg_names[instr.i.rs1], MAX_PART_LEN); |
|
| 453 | - | break; |
|
| 454 | - | ||
| 455 | - | case OP_STORE_FP: |
|
| 456 | - | /* F Extension - Floating-point store instruction */ |
|
| 457 | - | comp = parts('S'); |
|
| 458 | - | imm = get_s_imm(instr); |
|
| 459 | - | ||
| 460 | - | if (instr.s.funct3 == FUNCT3_WORD_FP) { |
|
| 461 | - | strncpy(comp.op, "fsw", MAX_PART_LEN); |
|
| 462 | - | } else { |
|
| 463 | - | strncpy( |
|
| 464 | - | comp.op, "fsw", MAX_PART_LEN |
|
| 465 | - | ); /* Only single precision supported */ |
|
| 466 | - | } |
|
| 467 | - | strncpy(comp.rs2, reg_names[instr.s.rs2], MAX_PART_LEN); |
|
| 468 | - | snprintf(comp.off, MAX_PART_LEN, "%d", imm); |
|
| 469 | - | strncpy(comp.base, reg_names[instr.s.rs1], MAX_PART_LEN); |
|
| 470 | - | break; |
|
| 471 | - | ||
| 472 | - | case OP_OP_FP: |
|
| 473 | - | /* F Extension - Floating-point operation instruction */ |
|
| 474 | - | comp = parts('R'); |
|
| 475 | - | ||
| 476 | - | switch (instr.r.funct7) { |
|
| 477 | - | case FUNCT7_FADD_S: |
|
| 478 | - | strncpy(comp.op, "fadd.s", MAX_PART_LEN); |
|
| 479 | - | break; |
|
| 480 | - | case FUNCT7_FSUB_S: |
|
| 481 | - | strncpy(comp.op, "fsub.s", MAX_PART_LEN); |
|
| 482 | - | break; |
|
| 483 | - | case FUNCT7_FMUL_S: |
|
| 484 | - | strncpy(comp.op, "fmul.s", MAX_PART_LEN); |
|
| 485 | - | break; |
|
| 486 | - | case FUNCT7_FDIV_S: |
|
| 487 | - | strncpy(comp.op, "fdiv.s", MAX_PART_LEN); |
|
| 488 | - | break; |
|
| 489 | - | case FUNCT7_FEQ_S: |
|
| 490 | - | /* Comparison instructions use different funct3 values */ |
|
| 491 | - | switch (instr.r.funct3) { |
|
| 492 | - | case FUNCT3_FEQ: |
|
| 493 | - | strncpy(comp.op, "feq.s", MAX_PART_LEN); |
|
| 494 | - | break; |
|
| 495 | - | case FUNCT3_FLT: |
|
| 496 | - | strncpy(comp.op, "flt.s", MAX_PART_LEN); |
|
| 497 | - | break; |
|
| 498 | - | case FUNCT3_FLE: |
|
| 499 | - | strncpy(comp.op, "fle.s", MAX_PART_LEN); |
|
| 500 | - | break; |
|
| 501 | - | default: |
|
| 502 | - | strncpy(comp.op, "fcmp.s", MAX_PART_LEN); |
|
| 503 | - | break; |
|
| 504 | - | } |
|
| 505 | - | break; |
|
| 506 | - | default: |
|
| 507 | - | strncpy(comp.op, "fop.s", MAX_PART_LEN); |
|
| 508 | - | break; |
|
| 509 | - | } |
|
| 510 | - | strncpy(comp.rd, reg_names[instr.r.rd], MAX_PART_LEN); |
|
| 511 | - | strncpy(comp.rs1, reg_names[instr.r.rs1], MAX_PART_LEN); |
|
| 512 | - | strncpy(comp.rs2, reg_names[instr.r.rs2], MAX_PART_LEN); |
|
| 513 | - | break; |
|
| 514 | - | ||
| 515 | 438 | default: |
|
| 516 | 439 | /* Unknown opcode */ |
|
| 517 | 440 | comp = parts('?'); |
|
| 518 | 441 | snprintf(comp.op, MAX_PART_LEN, "unknown (%d)", instr.i.opcode); |
|
| 519 | 442 | break; |