emu-rv32i.c 51.2 KB
Newer Older
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1
/* Modified by Shaos <me@shaos.net>
2
   22-NOV-2018 - Added saving HEX file with copy of the code in DEBUG_OUTPUT mode
Alexander Shabarshin's avatar
Alexander Shabarshin committed
3
   21-NOV-2018 - Some statistics added in DEBUG_EXTRA mode
Alexander Shabarshin's avatar
Alexander Shabarshin committed
4 5 6 7
   20-NOV-2018 - By default RV32M and RV32A extensions are disabled (STRICT_RV32I)
   18-NOV-2018 - Slghtly modified for pure C
   Source code taken from https://gist.github.com/FrankBuss/c974e59826d33e21d7cad54491ab50e8
*/
8

Alexander Shabarshin's avatar
Alexander Shabarshin committed
9 10 11 12 13 14 15 16 17 18 19 20 21
/*
RISCV emulator for the RV32I architecture
based on TinyEMU by Fabrice Bellard, see https://bellard.org/tinyemu/
stripped down for RV32I only, all "gotos" removed, and fixed some bugs for the compliance test
by Frank Buss, 2018

Requires libelf-dev:

sudo apt-get install libelf-dev


Compile it like this:

22
gcc -O3 -Wall -lelf emu-rv32i.c -o emu-rv32i
Alexander Shabarshin's avatar
Alexander Shabarshin committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41


It is compatible to Spike for the command line arguments, which means you can run
the compliance test from https://github.com/riscv/riscv-compliance like this:

make RISCV_TARGET=spike RISCV_DEVICE=rv32i TARGET_SIM=/full/path/emulator variant

It is also compatible with qemu32, as it is used for Zephyr. You can compile the
Zephyr examples for qemu like this:

cd zephyr
source zephyr-env.sh
cd samples/synchronization
mkdir build && cd build
cmake -GNinja -DBOARD=qemu_riscv32 ..
ninja

After this you can run it with the emulator like this:

42
emu-rv32i zephyr/zephyr.elf
Alexander Shabarshin's avatar
Alexander Shabarshin committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86


original copyright:
*/

/*
 * RISCV emulator
 *
 * Copyright (c) 2016 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#define XLEN 32

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <libelf.h>
#include <gelf.h>

Alexander Shabarshin's avatar
Alexander Shabarshin committed
87 88 89 90
/* uncomment this for an instruction trace and other debug outputs */
#if 0
#define DEBUG_OUTPUT
#endif
91 92 93
#if 0
#define DEBUG_EXTRA
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
94

Alexander Shabarshin's avatar
Alexander Shabarshin committed
95
/* added by Shaos: */
96
#define STRICT_RV32I
97 98 99
#define FALSE (0)
#define TRUE (-1)

Alexander Shabarshin's avatar
Alexander Shabarshin committed
100
#ifdef DEBUG_EXTRA
Alexander Shabarshin's avatar
Alexander Shabarshin committed
101 102
uint32_t minmemr,maxmemr,minmemw,maxmemw;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
103 104 105 106 107
int stats[64];

void init_stats(void)
{
  for(int i=0;i<64;i++) stats[i]=0;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
108 109
  minmemr = minmemw = ~0;
  maxmemr = maxmemw = 0;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
110 111 112 113 114
}

void print_stats(void)
{
  for(int i=0;i<64;i++) if(stats[i]) printf("stats[%i] = %i\n",i,stats[i]);
Alexander Shabarshin's avatar
Alexander Shabarshin committed
115
  printf("Memory Reading Area %x...%x\n",minmemr,maxmemr);
116 117
  if(minmemw+1) printf("Memory Writing Area %x...%x\n",minmemw,maxmemw);
  else printf("Memory Writing Area NONE\n");
Alexander Shabarshin's avatar
Alexander Shabarshin committed
118 119 120 121 122 123 124 125 126
}
#endif

#ifdef DEBUG_OUTPUT
#define dprintf(s) printf(s)
#else
#define dprintf(s)
#endif

Alexander Shabarshin's avatar
Alexander Shabarshin committed
127
/* memory mapped registers */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
128 129 130 131
#define MTIME_ADDR 0x40000000
#define MTIMECMP_ADDR 0x40000008
#define UART_TX_ADDR 0x40002000

Alexander Shabarshin's avatar
Alexander Shabarshin committed
132
/* emulate RAM */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
133 134 135
#define RAM_SIZE 0x10000
uint8_t ram[RAM_SIZE];

Alexander Shabarshin's avatar
Alexander Shabarshin committed
136
/* special memory mapped registers */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
137 138 139
uint64_t mtime;
uint64_t mtimecmp;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
140
/* virtual start address for index 0 in the ram array */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
141 142
uint32_t ram_start;

143 144 145 146
/* last byte of the memory initialized and temporary value */
uint32_t ram_last = 0;
uint32_t ram_curr = 0;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
147
/* used when called from the compliance tests */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
148 149 150
uint32_t begin_signature = 0;
uint32_t end_signature = 0;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
151
/* is set to false to exit the emulator */
152
int machine_running = TRUE;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
153

Alexander Shabarshin's avatar
Alexander Shabarshin committed
154
/* privilege levels */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
155 156 157 158 159
#define PRV_U 0
#define PRV_S 1
#define PRV_H 2
#define PRV_M 3

Alexander Shabarshin's avatar
Alexander Shabarshin committed
160
/* CPU state */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
uint32_t pc;
uint32_t next_pc;
uint32_t insn;
uint32_t reg[32];

uint8_t priv = PRV_M; /* see PRV_x */
uint8_t fs; /* MSTATUS_FS value */
uint8_t mxl; /* MXL field in MISA register */

uint64_t insn_counter;
int pending_exception; /* used during MMU exception handling */
uint32_t pending_tval;

/* CSRs */
uint32_t mstatus;
uint32_t mtvec;
uint32_t mscratch;
uint32_t mepc;
uint32_t mcause;
uint32_t mtval;
uint32_t mhartid; /* ro */
uint32_t misa;
uint32_t mie;
uint32_t mip;
uint32_t medeleg;
uint32_t mideleg;
uint32_t mcounteren;

uint32_t stvec;
uint32_t sscratch;
uint32_t sepc;
uint32_t scause;
uint32_t stval;
uint32_t satp;
uint32_t scounteren;
uint32_t load_res; /* for atomic LR/SC */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
198
/* exception causes */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
#define CAUSE_MISALIGNED_FETCH    0x0
#define CAUSE_FAULT_FETCH         0x1
#define CAUSE_ILLEGAL_INSTRUCTION 0x2
#define CAUSE_BREAKPOINT          0x3
#define CAUSE_MISALIGNED_LOAD     0x4
#define CAUSE_FAULT_LOAD          0x5
#define CAUSE_MISALIGNED_STORE    0x6
#define CAUSE_FAULT_STORE         0x7
#define CAUSE_USER_ECALL          0x8
#define CAUSE_SUPERVISOR_ECALL    0x9
#define CAUSE_HYPERVISOR_ECALL    0xa
#define CAUSE_MACHINE_ECALL       0xb
#define CAUSE_FETCH_PAGE_FAULT    0xc
#define CAUSE_LOAD_PAGE_FAULT     0xd
#define CAUSE_STORE_PAGE_FAULT    0xf
#define CAUSE_INTERRUPT  ((uint32_t)1 << 31)

/* misa CSR */
#define MCPUID_SUPER   (1 << ('S' - 'A'))
#define MCPUID_USER    (1 << ('U' - 'A'))
#define MCPUID_I       (1 << ('I' - 'A'))
#define MCPUID_M       (1 << ('M' - 'A'))
#define MCPUID_A       (1 << ('A' - 'A'))
#define MCPUID_F       (1 << ('F' - 'A'))
#define MCPUID_D       (1 << ('D' - 'A'))
#define MCPUID_Q       (1 << ('Q' - 'A'))
#define MCPUID_C       (1 << ('C' - 'A'))

#define MIP_USIP (1 << 0)
#define MIP_SSIP (1 << 1)
#define MIP_HSIP (1 << 2)
#define MIP_MSIP (1 << 3)
#define MIP_UTIP (1 << 4)
#define MIP_STIP (1 << 5)
#define MIP_HTIP (1 << 6)
#define MIP_MTIP (1 << 7)
#define MIP_UEIP (1 << 8)
#define MIP_SEIP (1 << 9)
#define MIP_HEIP (1 << 10)
#define MIP_MEIP (1 << 11)

/* mstatus CSR */
#define MSTATUS_SPIE_SHIFT 5
#define MSTATUS_MPIE_SHIFT 7
#define MSTATUS_SPP_SHIFT 8
#define MSTATUS_MPP_SHIFT 11
#define MSTATUS_FS_SHIFT 13
#define MSTATUS_UXL_SHIFT 32
#define MSTATUS_SXL_SHIFT 34

#define MSTATUS_UIE (1 << 0)
#define MSTATUS_SIE (1 << 1)
#define MSTATUS_HIE (1 << 2)
#define MSTATUS_MIE (1 << 3)
#define MSTATUS_UPIE (1 << 4)
#define MSTATUS_SPIE (1 << MSTATUS_SPIE_SHIFT)
#define MSTATUS_HPIE (1 << 6)
#define MSTATUS_MPIE (1 << MSTATUS_MPIE_SHIFT)
#define MSTATUS_SPP (1 << MSTATUS_SPP_SHIFT)
#define MSTATUS_HPP (3 << 9)
#define MSTATUS_MPP (3 << MSTATUS_MPP_SHIFT)
#define MSTATUS_FS (3 << MSTATUS_FS_SHIFT)
#define MSTATUS_XS (3 << 15)
#define MSTATUS_MPRV (1 << 17)
#define MSTATUS_SUM (1 << 18)
#define MSTATUS_MXR (1 << 19)
#define MSTATUS_UXL_MASK ((uint64_t)3 << MSTATUS_UXL_SHIFT)
#define MSTATUS_SXL_MASK ((uint64_t)3 << MSTATUS_SXL_SHIFT)

int ctz32(uint32_t a)
{
    int i;
    if (a == 0)
        return 32;
    for(i = 0; i < 32; i++) {
        if ((a >> i) & 1)
            return i;
    }
    return 32;
}

#define SSTATUS_MASK0 (MSTATUS_UIE | MSTATUS_SIE |       \
                      MSTATUS_UPIE | MSTATUS_SPIE |     \
                      MSTATUS_SPP | \
                      MSTATUS_FS | MSTATUS_XS | \
                      MSTATUS_SUM | MSTATUS_MXR)
#define SSTATUS_MASK SSTATUS_MASK0


#define MSTATUS_MASK (MSTATUS_UIE | MSTATUS_SIE | MSTATUS_MIE |      \
                      MSTATUS_UPIE | MSTATUS_SPIE | MSTATUS_MPIE |    \
                      MSTATUS_SPP | MSTATUS_MPP | \
                      MSTATUS_FS | \
                      MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_MXR)

/* cycle and insn counters */
#define COUNTEREN_MASK ((1 << 0) | (1 << 2))

/* return the complete mstatus with the SD bit */
uint32_t get_mstatus(uint32_t mask)
{
    uint32_t val;
301
    int sd;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    val = mstatus | (fs << MSTATUS_FS_SHIFT);
    val &= mask;
    sd = ((val & MSTATUS_FS) == MSTATUS_FS) |
         ((val & MSTATUS_XS) == MSTATUS_XS);
    if (sd)
        val |= (uint32_t)1 << (XLEN - 1);
    return val;
}

void set_mstatus(uint32_t val)
{
    fs = (val >> MSTATUS_FS_SHIFT) & 3;

    uint32_t mask = MSTATUS_MASK & ~MSTATUS_FS;
    mstatus = (mstatus & ~mask) | (val & mask);
}

void invalid_csr(uint32_t *pval, uint32_t csr)
{
    /* the 'time' counter is usually emulated */
    if (csr != 0xc01 && csr != 0xc81) {
#ifdef DEBUG_OUTPUT
        printf("csr_read: invalid CSR=0x%x\n", csr);
#endif
    }
    *pval = 0;
}

/* return -1 if invalid CSR. 0 if OK. 'will_write' indicate that the
   csr will be written after (used for CSR access check) */
int csr_read(uint32_t *pval, uint32_t csr,
333
             int will_write)
Alexander Shabarshin's avatar
Alexander Shabarshin committed
334 335 336
{
    uint32_t val;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
337 338 339 340
#ifdef DEBUG_EXTRA
    printf("csr_read: csr=0x%03x %i\n", csr, will_write);
#endif

Alexander Shabarshin's avatar
Alexander Shabarshin committed
341 342 343 344 345 346
    if (((csr & 0xc00) == 0xc00) && will_write)
        return -1; /* read-only CSR */
    if (priv < ((csr >> 8) & 3))
        return -1; /* not enough priviledge */

    switch(csr) {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
347 348
    case 0xc00: /* cycle */
    case 0xc02: /* instret */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    {
        uint32_t counteren;
        if (priv < PRV_M) {
            if (priv < PRV_S)
                counteren = scounteren;
            else
                counteren = mcounteren;
            if (((counteren >> (csr & 0x1f)) & 1) == 0) {
                invalid_csr(pval, csr);
                return -1;
            }
        }
    }
    val = (int64_t)insn_counter;
    break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
364 365
    case 0xc80: /* cycleh */
    case 0xc82: /* instreth */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    {
        uint32_t counteren;
        if (priv < PRV_M) {
            if (priv < PRV_S)
                counteren = scounteren;
            else
                counteren = mcounteren;
            if (((counteren >> (csr & 0x1f)) & 1) == 0) {
                invalid_csr(pval, csr);
                return -1;
            }
        }
    }
    val = insn_counter >> 32;
    break;

    case 0x100:
        val = get_mstatus(SSTATUS_MASK);
        break;
    case 0x104: /* sie */
        val = mie & mideleg;
        break;
    case 0x105:
        val = stvec;
        break;
    case 0x106:
        val = scounteren;
        break;
    case 0x140:
        val = sscratch;
        break;
    case 0x141:
        val = sepc;
        break;
    case 0x142:
        val = scause;
        break;
    case 0x143:
        val = stval;
        break;
    case 0x144: /* sip */
        val = mip & mideleg;
        break;
    case 0x180:
        val = satp;
        break;
    case 0x300:
        val = get_mstatus((uint32_t)-1);
        break;
    case 0x301:
        val = misa;
        val |= (uint32_t)mxl << (XLEN - 2);
        break;
    case 0x302:
        val = medeleg;
        break;
    case 0x303:
        val = mideleg;
        break;
    case 0x304:
        val = mie;
        break;
    case 0x305:
        val = mtvec;
        break;
    case 0x306:
        val = mcounteren;
        break;
    case 0x340:
        val = mscratch;
        break;
    case 0x341:
        val = mepc;
        break;
    case 0x342:
        val = mcause;
        break;
    case 0x343:
        val = mtval;
        break;
    case 0x344:
        val = mip;
        break;
    case 0xb00: /* mcycle */
    case 0xb02: /* minstret */
        val = (int64_t)insn_counter;
        break;
    case 0xb80: /* mcycleh */
    case 0xb82: /* minstreth */
        val = insn_counter >> 32;
        break;
    case 0xf14:
        val = mhartid;
        break;
    default:
        invalid_csr(pval, csr);
Alexander Shabarshin's avatar
Alexander Shabarshin committed
462
        /* return -1; */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
463 464
        return 0;
    }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
465 466 467 468 469

#ifdef DEBUG_EXTRA
    printf("csr_read: csr=0x%03x --> 0x%08x\n", csr, val);
#endif

Alexander Shabarshin's avatar
Alexander Shabarshin committed
470 471 472 473 474 475 476 477 478
    *pval = val;
    return 0;
}

/* return -1 if invalid CSR, 0 if OK, 1 if the interpreter loop must be
   exited (e.g. XLEN was modified), 2 if TLBs have been flushed. */
int csr_write(uint32_t csr, uint32_t val)
{
    uint32_t mask;
479
#ifdef DEBUG_EXTRA
Alexander Shabarshin's avatar
Alexander Shabarshin committed
480
    printf("csr_write: csr=0x%03x val=0x%08x\n", csr, val);
Alexander Shabarshin's avatar
Alexander Shabarshin committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
#endif
    switch(csr) {
    case 0x100: /* sstatus */
        set_mstatus((mstatus & ~SSTATUS_MASK) | (val & SSTATUS_MASK));
        break;
    case 0x104: /* sie */
        mask = mideleg;
        mie = (mie & ~mask) | (val & mask);
        break;
    case 0x105:
        stvec = val & ~3;
        break;
    case 0x106:
        scounteren = val & COUNTEREN_MASK;
        break;
    case 0x140:
        sscratch = val;
        break;
    case 0x141:
        sepc = val & ~1;
        break;
    case 0x142:
        scause = val;
        break;
    case 0x143:
        stval = val;
        break;
    case 0x144: /* sip */
        mask = mideleg;
        mip = (mip & ~mask) | (val & mask);
        break;
512 513 514 515 516 517 518 519
    case 0x180: /* no ASID implemented */
        {
            int new_mode;
            new_mode = (val >> 31) & 1;
            satp = (val & (((uint32_t)1 << 22) - 1)) |
                   (new_mode << 31);
        }
        return 2;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

    case 0x300:
        set_mstatus(val);
        break;
    case 0x301: /* misa */
        break;
    case 0x302:
        mask = (1 << (CAUSE_STORE_PAGE_FAULT + 1)) - 1;
        medeleg = (medeleg & ~mask) | (val & mask);
        break;
    case 0x303:
        mask = MIP_SSIP | MIP_STIP | MIP_SEIP;
        mideleg = (mideleg & ~mask) | (val & mask);
        break;
    case 0x304:
        mask = MIP_MSIP | MIP_MTIP | MIP_SSIP | MIP_STIP | MIP_SEIP;
        mie = (mie & ~mask) | (val & mask);
        break;
    case 0x305:
        mtvec = val & ~3;
        break;
    case 0x306:
        mcounteren = val & COUNTEREN_MASK;
        break;
    case 0x340:
        mscratch = val;
        break;
    case 0x341:
        mepc = val & ~1;
        break;
    case 0x342:
        mcause = val;
        break;
    case 0x343:
        mtval = val;
        break;
    case 0x344:
        mask = MIP_SSIP | MIP_STIP;
        mip = (mip & ~mask) | (val & mask);
        break;
    default:
        return 0;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
562
        /* return -1; */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
    }
    return 0;
}

void handle_sret()
{
    int spp, spie;
    spp = (mstatus >> MSTATUS_SPP_SHIFT) & 1;
    /* set the IE state to previous IE state */
    spie = (mstatus >> MSTATUS_SPIE_SHIFT) & 1;
    mstatus = (mstatus & ~(1 << spp)) |
              (spie << spp);
    /* set SPIE to 1 */
    mstatus |= MSTATUS_SPIE;
    /* set SPP to U */
    mstatus &= ~MSTATUS_SPP;
    priv = spp;
    next_pc = sepc;
}

void handle_mret()
{
    int mpp, mpie;
    mpp = (mstatus >> MSTATUS_MPP_SHIFT) & 3;
    /* set the IE state to previous IE state */
    mpie = (mstatus >> MSTATUS_MPIE_SHIFT) & 1;
    mstatus = (mstatus & ~(1 << mpp)) |
              (mpie << mpp);
    /* set MPIE to 1 */
    mstatus |= MSTATUS_MPIE;
    /* set MPP to U */
    mstatus &= ~MSTATUS_MPP;
    priv = mpp;
    next_pc = mepc;
}

Alexander Shabarshin's avatar
Alexander Shabarshin committed
599
void raise_exception(uint32_t cause, uint32_t tval)
Alexander Shabarshin's avatar
Alexander Shabarshin committed
600
{
601
    int deleg;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
602

Alexander Shabarshin's avatar
Alexander Shabarshin committed
603
    /* exit for Zephyr applications */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
604
    if (cause == CAUSE_ILLEGAL_INSTRUCTION) {
605 606 607
#ifdef DEBUG_OUTPUT
        printf("raise_exception: illegal instruction 0x%x 0x%x\n", cause, tval);
#endif
608
        machine_running = FALSE;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
        return;
    }

    if (priv <= PRV_S) {
        /* delegate the exception to the supervisor priviledge */
        if (cause & CAUSE_INTERRUPT)
            deleg = (mideleg >> (cause & (XLEN - 1))) & 1;
        else
            deleg = (medeleg >> cause) & 1;
    } else {
        deleg = 0;
    }

    if (deleg) {
        scause = cause;
        sepc = pc;
        stval = tval;
        mstatus = (mstatus & ~MSTATUS_SPIE) |
                  (((mstatus >> priv) & 1) << MSTATUS_SPIE_SHIFT);
        mstatus = (mstatus & ~MSTATUS_SPP) |
                  (priv << MSTATUS_SPP_SHIFT);
        mstatus &= ~MSTATUS_SIE;
        priv = PRV_S;
        next_pc = stvec;
    } else {
        mcause = cause;
        mepc = pc;
        mtval = tval;
        mstatus = (mstatus & ~MSTATUS_MPIE) |
                  (((mstatus >> priv) & 1) << MSTATUS_MPIE_SHIFT);
        mstatus = (mstatus & ~MSTATUS_MPP) |
                  (priv << MSTATUS_MPP_SHIFT);
        mstatus &= ~MSTATUS_MIE;
        priv = PRV_M;
        next_pc = mtvec;
    }
}

uint32_t get_pending_irq_mask()
{
    uint32_t pending_ints, enabled_ints;

    pending_ints = mip & mie;
    if (pending_ints == 0)
        return 0;

    enabled_ints = 0;
    switch(priv) {
    case PRV_M:
        if (mstatus & MSTATUS_MIE)
            enabled_ints = ~mideleg;
        break;
    case PRV_S:
        enabled_ints = ~mideleg;
        if (mstatus & MSTATUS_SIE)
            enabled_ints |= mideleg;
        break;
    default:
    case PRV_U:
        enabled_ints = -1;
        break;
    }
    return pending_ints & enabled_ints;
}

int raise_interrupt()
{
    uint32_t mask;
    int irq_num;

    mask = get_pending_irq_mask();
    if (mask == 0)
        return 0;
    irq_num = ctz32(mask);
    raise_exception(irq_num | CAUSE_INTERRUPT, 0);
    return -1;
}

687 688
/* read 32-bit instruction from memory by PC */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
689 690
uint32_t get_insn32(uint32_t pc)
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
691 692 693 694
#ifdef DEBUG_EXTRA
    if(pc && pc < minmemr) minmemr = pc;
    if(pc+3 > maxmemr) maxmemr = pc+3;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
695 696 697 698 699 700
    uint32_t ptr = pc - ram_start;
    if (ptr > RAM_SIZE) return 1;
    uint8_t* p = ram + ptr;
    return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}

701 702
/* read 8-bit data from memory */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
703 704
int target_read_u8(uint8_t *pval, uint32_t addr)
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
705 706 707 708
#ifdef DEBUG_EXTRA
    if((addr>>28)!=4 && addr < minmemr) minmemr = addr;
    if(addr > maxmemr) maxmemr = addr;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
709 710 711 712 713 714 715 716 717 718 719 720
    addr -= ram_start;
    if (addr > RAM_SIZE) {
        *pval = 0;
        printf("illegal read 8, PC: 0x%08x, address: 0x%08x\n", pc, addr + ram_start);
        return 1;
    } else {
        uint8_t* p = ram + addr;
        *pval = p[0];
    }
    return 0;
}

721 722
/* read 16-bit data from memory */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
723 724
int target_read_u16(uint16_t *pval, uint32_t addr)
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
725 726 727 728
#ifdef DEBUG_EXTRA
    if((addr>>28)!=4 && addr < minmemr) minmemr = addr;
    if(addr+1 > maxmemr) maxmemr = addr+1;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    if (addr & 1) {
        pending_exception = CAUSE_MISALIGNED_LOAD;
        pending_tval = addr;
        return 1;
    }
    addr -= ram_start;
    if (addr > RAM_SIZE)  {
        *pval = 0;
        printf("illegal read 16, PC: 0x%08x, address: 0x%08x\n", pc, addr + ram_start);
        return 1;
    } else {
        uint8_t* p = ram + addr;
        *pval = p[0] | (p[1] << 8);
    }
    return 0;
}

746 747
/* read 32-bit data from memory */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
748 749
int target_read_u32(uint32_t *pval, uint32_t addr)
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
750 751 752 753
#ifdef DEBUG_EXTRA
    if((addr>>28)!=4 && addr < minmemr) minmemr = addr;
    if(addr+3 > maxmemr) maxmemr = addr+3;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    if (addr & 3) {
        pending_exception = CAUSE_MISALIGNED_LOAD;
        pending_tval = addr;
        return 1;
    }
    if (addr == MTIMECMP_ADDR) {
        *pval = (uint32_t) mtimecmp;
    } else if (addr == MTIMECMP_ADDR + 4) {
        *pval = (uint32_t) (mtimecmp >> 32);
    } else if (addr == MTIME_ADDR) {
        *pval = (uint32_t) mtime;
    } else if (addr == MTIME_ADDR + 4) {
        *pval = (uint32_t) (mtime >> 32);
    } else {
        addr -= ram_start;
        if (addr > RAM_SIZE) {
            *pval = 0;
            printf("illegal read 32, PC: 0x%08x, address: 0x%08x\n", pc, addr + ram_start);
            return 1;
        } else {
            uint8_t* p = ram + addr;
            *pval = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
        }
    }
    return 0;
}

781 782
/* write 8-bit data to memory */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
783 784
int target_write_u8(uint32_t addr, uint8_t val)
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
785 786 787 788
#ifdef DEBUG_EXTRA
    if((addr>>28)!=4 && addr < minmemw) minmemw = addr;
    if(addr > maxmemw) maxmemw = addr;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
789
    if (addr == UART_TX_ADDR) {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
790
        /* test for UART output, compatible with QEMU */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804
        printf("%c", val);
    } else {
        addr -= ram_start;
        if (addr > RAM_SIZE - 1) {
            printf("illegal write 8, PC: 0x%08x, address: 0x%08x\n", pc, addr + ram_start);
            return 1;
        } else {
            uint8_t* p = ram + addr;
            p[0] = val & 0xff;
        }
    }
    return 0;
}

805 806
/* write 16-bit data to memory */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
807 808
int target_write_u16(uint32_t addr, uint16_t val)
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
809 810 811 812
#ifdef DEBUG_EXTRA
    if((addr>>28)!=4 && addr < minmemw) minmemw = addr;
    if(addr+1 > maxmemw) maxmemw = addr+1;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
    if (addr & 1) {
        pending_exception = CAUSE_MISALIGNED_STORE;
        pending_tval = addr;
        return 1;
    }
    addr -= ram_start;
    if (addr > RAM_SIZE - 2) {
        printf("illegal write 16, PC: 0x%08x, address: 0x%08x\n", pc, addr + ram_start);
        return 1;
    } else {
        uint8_t* p = ram + addr;
        p[0] = val & 0xff;
        p[1] = (val >> 8) & 0xff;
    }
    return 0;
}

830
/* write 32-bit data to memory */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
831 832 833

int target_write_u32(uint32_t addr, uint32_t val)
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
834 835 836 837
#ifdef DEBUG_EXTRA
    if((addr>>28)!=4 && addr < minmemw) minmemw = addr;
    if(addr+3 > maxmemw) maxmemw = addr+3;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
838 839 840 841 842 843
    if (addr & 3) {
        pending_exception = CAUSE_MISALIGNED_STORE;
        pending_tval = addr;
        return 1;
    }
    if (addr == MTIMECMP_ADDR) {
844 845
        mtimecmp = (mtimecmp & 0xffffffff00000000ll) | val;
        mip &= ~MIP_MTIP;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
846
    } else if (addr == MTIMECMP_ADDR + 4) {
847 848
        mtimecmp = (mtimecmp & 0xffffffffll) | (((uint64_t)val) << 32);
        mip &= ~MIP_MTIP;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
    } else {
        addr -= ram_start;
        if (addr > RAM_SIZE - 4)  {
            return 1;
        } else {
            uint8_t* p = ram + addr;
            p[0] = val & 0xff;
            p[1] = (val >> 8) & 0xff;
            p[2] = (val >> 16) & 0xff;
            p[3] = (val >> 24) & 0xff;
        }
    }
    return 0;
}

864 865
#ifndef STRICT_RV32I

Alexander Shabarshin's avatar
Alexander Shabarshin committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
int32_t div32(int32_t a, int32_t b)
{
    if (b == 0) {
        return -1;
    } else if (a == ((int32_t)1 << (XLEN - 1)) && b == -1) {
        return a;
    } else {
        return a / b;
    }
}

uint32_t divu32(uint32_t a, uint32_t b)
{
    if (b == 0) {
        return -1;
    } else {
        return a / b;
    }
}

int32_t rem32(int32_t a, int32_t b)
{
    if (b == 0) {
        return a;
    } else if (a == ((int32_t)1 << (XLEN - 1)) && b == -1) {
        return 0;
    } else {
        return a % b;
    }
}

uint32_t remu32(uint32_t a, uint32_t b)
{
    if (b == 0) {
        return a;
    } else {
        return a % b;
    }
}

static uint32_t mulh32(int32_t a, int32_t b)
{
    return ((int64_t)a * (int64_t)b) >> 32;
}

static uint32_t mulhsu32(int32_t a, uint32_t b)
{
    return ((int64_t)a * (int64_t)b) >> 32;
}

static uint32_t mulhu32(uint32_t a, uint32_t b)
{
    return ((int64_t)a * (int64_t)b) >> 32;
}

921 922
#endif

923 924
#ifdef DEBUG_EXTRA

Alexander Shabarshin's avatar
Alexander Shabarshin committed
925
/* dumps all registers, useful for in-depth debugging */
926

Alexander Shabarshin's avatar
Alexander Shabarshin committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
static void dump_regs()
{
    printf("x0 zero: %08x\n", reg[0]);
    printf("x1 ra:   %08x\n", reg[1]);
    printf("x2 sp:   %08x\n", reg[2]);
    printf("x3 gp:   %08x\n", reg[3]);
    printf("x4 tp:   %08x\n", reg[4]);
    printf("x5 t0:   %08x\n", reg[5]);
    printf("x6 t1:   %08x\n", reg[6]);
    printf("x7 t2:   %08x\n", reg[7]);
    printf("x8 s0:   %08x\n", reg[8]);
    printf("x9 s1:   %08x\n", reg[9]);
    printf("x10 a0:  %08x\n", reg[10]);
    printf("x11 a1:  %08x\n", reg[11]);
    printf("x12 a2:  %08x\n", reg[12]);
    printf("x13 a3:  %08x\n", reg[13]);
    printf("x14 a4:  %08x\n", reg[14]);
    printf("x15 a5:  %08x\n", reg[15]);
    printf("x16 a6:  %08x\n", reg[16]);
    printf("x17 a7:  %08x\n", reg[17]);
    printf("x18 s2:  %08x\n", reg[18]);
    printf("x19 s3:  %08x\n", reg[19]);
    printf("x20 s4:  %08x\n", reg[20]);
    printf("x21 s5:  %08x\n", reg[21]);
    printf("x22 s6:  %08x\n", reg[22]);
    printf("x23 s7:  %08x\n", reg[23]);
    printf("x24 s8:  %08x\n", reg[24]);
    printf("x25 s9:  %08x\n", reg[25]);
    printf("x26 s10: %08x\n", reg[26]);
    printf("x27 s11: %08x\n", reg[27]);
    printf("x28 t3:  %08x\n", reg[28]);
    printf("x29 t4:  %08x\n", reg[29]);
    printf("x30 t5:  %08x\n", reg[30]);
    printf("x31 t6:  %08x\n", reg[31]);
}
962

Alexander Shabarshin's avatar
Alexander Shabarshin committed
963
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
964 965 966 967 968

void execute_instruction()
{
    uint32_t opcode, rd, rs1, rs2, funct3;
    int32_t imm, cond, err;
969
    uint32_t addr, val=0, val2;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
970 971 972 973 974 975 976

    opcode = insn & 0x7f;
    rd = (insn >> 7) & 0x1f;
    rs1 = (insn >> 15) & 0x1f;
    rs2 = (insn >> 20) & 0x1f;

    switch(opcode) {
977

Alexander Shabarshin's avatar
Alexander Shabarshin committed
978
    case 0x37: /* lui */
979

Alexander Shabarshin's avatar
Alexander Shabarshin committed
980 981 982 983
#ifdef DEBUG_EXTRA
        dprintf(">>> LUI\n");
        stats[0]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
984 985 986
        if (rd != 0)
            reg[rd] = (int32_t)(insn & 0xfffff000);
        break;
987

Alexander Shabarshin's avatar
Alexander Shabarshin committed
988
    case 0x17: /* auipc */
989

Alexander Shabarshin's avatar
Alexander Shabarshin committed
990 991 992 993
#ifdef DEBUG_EXTRA
        dprintf(">>> AUIPC\n");
        stats[1]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
994 995 996
        if (rd != 0)
            reg[rd] = (int32_t)(pc + (int32_t)(insn & 0xfffff000));
        break;
997

Alexander Shabarshin's avatar
Alexander Shabarshin committed
998
    case 0x6f: /* jal */
999

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1000 1001 1002 1003
#ifdef DEBUG_EXTRA
        dprintf(">>> JAL\n");
        stats[2]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1004 1005 1006 1007 1008 1009 1010 1011 1012
        imm = ((insn >> (31 - 20)) & (1 << 20)) |
              ((insn >> (21 - 1)) & 0x7fe) |
              ((insn >> (20 - 11)) & (1 << 11)) |
              (insn & 0xff000);
        imm = (imm << 11) >> 11;
        if (rd != 0)
            reg[rd] = pc + 4;
        next_pc = (int32_t)(pc + imm);
        break;
1013

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1014
    case 0x67: /* jalr */
1015

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1016 1017 1018 1019
#ifdef DEBUG_EXTRA
        dprintf(">>> JALR\n");
        stats[3]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1020 1021 1022 1023 1024 1025
        imm = (int32_t)insn >> 20;
        val = pc + 4;
        next_pc = (int32_t)(reg[rs1] + imm) & ~1;
        if (rd != 0)
            reg[rd] = val;
        break;
1026 1027 1028

    case 0x63: /* BRANCH */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1029 1030 1031
        funct3 = (insn >> 12) & 7;
        switch(funct3 >> 1) {
        case 0: /* beq/bne */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1032 1033 1034 1035 1036 1037 1038 1039 1040
#ifdef DEBUG_EXTRA
            if(!(funct3 & 1)) {
              dprintf(">>> BEQ\n");
              stats[4]++;
            } else  {
              dprintf(">>> BNE\n");
              stats[5]++;
            }
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1041 1042 1043
            cond = (reg[rs1] == reg[rs2]);
            break;
        case 2: /* blt/bge */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1044 1045 1046 1047 1048 1049 1050 1051 1052
#ifdef DEBUG_EXTRA
            if(!(funct3 & 1)) {
              dprintf(">>> BLT\n");
              stats[6]++;
            } else  {
              dprintf(">>> BGE\n");
              stats[7]++;
            }
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1053 1054 1055
            cond = ((int32_t)reg[rs1] < (int32_t)reg[rs2]);
            break;
        case 3: /* bltu/bgeu */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1056 1057 1058 1059 1060 1061 1062 1063 1064
#ifdef DEBUG_EXTRA
            if(!(funct3 & 1)) {
              dprintf(">>> BLTU\n");
              stats[8]++;
            } else  {
              dprintf(">>> BGEU\n");
              stats[9]++;
            }
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
            cond = (reg[rs1] < reg[rs2]);
            break;
        default:
            raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
            return;
        }
        cond ^= (funct3 & 1);
        if (cond) {
            imm = ((insn >> (31 - 12)) & (1 << 12)) |
                  ((insn >> (25 - 5)) & 0x7e0) |
                  ((insn >> (8 - 1)) & 0x1e) |
                  ((insn << (11 - 7)) & (1 << 11));
            imm = (imm << 19) >> 19;
            next_pc = (int32_t)(pc + imm);
            break;
        }
        break;
1082 1083 1084

    case 0x03: /* LOAD */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1085 1086 1087 1088
        funct3 = (insn >> 12) & 7;
        imm = (int32_t)insn >> 20;
        addr = reg[rs1] + imm;
        switch(funct3) {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1089

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1090 1091
        case 0: /* lb */
        {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1092 1093 1094 1095
#ifdef DEBUG_EXTRA
            dprintf(">>> LB\n");
            stats[10]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1096 1097 1098 1099 1100 1101 1102 1103
            uint8_t rval;
            if (target_read_u8(&rval, addr)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            val = (int8_t)rval;
        }
        break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1104

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1105 1106
        case 1: /* lh */
        {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1107 1108 1109 1110
#ifdef DEBUG_EXTRA
            dprintf(">>> LH\n");
            stats[11]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1111 1112 1113 1114 1115 1116 1117 1118
            uint16_t rval;
            if (target_read_u16(&rval, addr)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            val = (int16_t)rval;
        }
        break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1119

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1120 1121
        case 2: /* lw */
        {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1122 1123 1124 1125
#ifdef DEBUG_EXTRA
            dprintf(">>> LW\n");
            stats[12]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1126 1127 1128 1129 1130 1131 1132 1133
            uint32_t rval;
            if (target_read_u32(&rval, addr)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            val = (int32_t)rval;
        }
        break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1134

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1135 1136
        case 4: /* lbu */
        {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1137 1138 1139 1140
#ifdef DEBUG_EXTRA
            dprintf(">>> LBU\n");
            stats[13]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1141 1142 1143 1144 1145 1146 1147 1148
            uint8_t rval;
            if (target_read_u8(&rval, addr)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            val = rval;
        }
        break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1149

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1150 1151
        case 5: /* lhu */
        {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1152 1153 1154 1155
#ifdef DEBUG_EXTRA
            dprintf(">>> LHU\n");
            stats[14]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1156 1157 1158 1159 1160 1161 1162 1163
            uint16_t rval;
            if (target_read_u16(&rval, addr)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            val = rval;
        }
        break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1164

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1165 1166 1167 1168 1169 1170 1171
        default:
            raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
            return;
        }
        if (rd != 0)
            reg[rd] = val;
        break;
1172 1173 1174

    case 0x23: /* STORE */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1175 1176 1177 1178 1179 1180
        funct3 = (insn >> 12) & 7;
        imm = rd | ((insn >> (25 - 5)) & 0xfe0);
        imm = (imm << 20) >> 20;
        addr = reg[rs1] + imm;
        val = reg[rs2];
        switch(funct3) {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1181

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1182
        case 0: /* sb */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1183 1184 1185 1186
#ifdef DEBUG_EXTRA
            dprintf(">>> SB\n");
            stats[15]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1187 1188 1189 1190 1191
            if (target_write_u8(addr, val)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1192

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1193
        case 1: /* sh */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1194 1195 1196 1197
#ifdef DEBUG_EXTRA
            dprintf(">>> SH\n");
            stats[16]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1198 1199 1200 1201 1202
            if (target_write_u16(addr, val)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1203

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1204
        case 2: /* sw */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1205 1206 1207 1208
#ifdef DEBUG_EXTRA
            dprintf(">>> SW\n");
            stats[17]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1209 1210 1211 1212 1213
            if (target_write_u32(addr, val)) {
                raise_exception(pending_exception, pending_tval);
                return;
            }
            break;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1214

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1215 1216 1217 1218 1219
        default:
            raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
            return;
        }
        break;
1220 1221 1222

    case 0x13: /* OP-IMM */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1223 1224 1225 1226
        funct3 = (insn >> 12) & 7;
        imm = (int32_t)insn >> 20;
        switch(funct3) {
        case 0: /* addi */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1227 1228 1229 1230
#ifdef DEBUG_EXTRA
            dprintf(">>> ADDI\n");
            stats[18]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1231 1232 1233
            val = (int32_t)(reg[rs1] + imm);
            break;
        case 1: /* slli */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1234 1235 1236 1237
#ifdef DEBUG_EXTRA
            dprintf(">>> SLLI\n");
            stats[24]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1238 1239 1240 1241 1242 1243 1244
            if ((imm & ~(XLEN - 1)) != 0) {
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            val = (int32_t)(reg[rs1] << (imm & (XLEN - 1)));
            break;
        case 2: /* slti */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1245 1246 1247 1248
#ifdef DEBUG_EXTRA
            dprintf(">>> SLTI\n");
            stats[19]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1249 1250 1251
            val = (int32_t)reg[rs1] < (int32_t)imm;
            break;
        case 3: /* sltiu */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1252 1253 1254 1255
#ifdef DEBUG_EXTRA
            dprintf(">>> SLTIU\n");
            stats[20]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1256 1257 1258
            val = reg[rs1] < (uint32_t)imm;
            break;
        case 4: /* xori */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1259 1260 1261 1262
#ifdef DEBUG_EXTRA
            dprintf(">>> XORI\n");
            stats[21]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1263 1264 1265 1266 1267 1268 1269 1270
            val = reg[rs1] ^ imm;
            break;
        case 5: /* srli/srai */
            if ((imm & ~((XLEN - 1) | 0x400)) != 0) {
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            if (imm & 0x400)
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1271 1272 1273 1274 1275
            {
#ifdef DEBUG_EXTRA
                dprintf(">>> SRAI\n");
                stats[26]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1276
                val = (int32_t)reg[rs1] >> (imm & (XLEN - 1));
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1277
            }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1278
            else
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1279 1280 1281 1282 1283
            {
#ifdef DEBUG_EXTRA
                dprintf(">>> SRLI\n");
                stats[25]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1284
                val = (int32_t)((uint32_t)reg[rs1] >> (imm & (XLEN - 1)));
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1285
            }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1286 1287
            break;
        case 6: /* ori */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1288 1289 1290 1291
#ifdef DEBUG_EXTRA
            dprintf(">>> ORI\n");
            stats[22]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1292 1293 1294
            val = reg[rs1] | imm;
            break;
        case 7: /* andi */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1295 1296 1297 1298
#ifdef DEBUG_EXTRA
            dprintf(">>> ANDI\n");
            stats[23]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1299 1300 1301 1302 1303 1304
            val = reg[rs1] & imm;
            break;
        }
        if (rd != 0)
            reg[rd] = val;
        break;
1305 1306 1307

    case 0x33: /* OP */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1308 1309 1310
        imm = insn >> 25;
        val = reg[rs1];
        val2 = reg[rs2];
1311
#ifndef STRICT_RV32I
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
        if (imm == 1) {
            funct3 = (insn >> 12) & 7;
            switch(funct3) {
            case 0: /* mul */
                val = (int32_t)((int32_t)val * (int32_t)val2);
                break;
            case 1: /* mulh */
                val = (int32_t)mulh32(val, val2);
                break;
            case 2:/* mulhsu */
                val = (int32_t)mulhsu32(val, val2);
                break;
            case 3:/* mulhu */
                val = (int32_t)mulhu32(val, val2);
                break;
            case 4:/* div */
                val = div32(val, val2);
                break;
            case 5:/* divu */
                val = (int32_t)divu32(val, val2);
                break;
            case 6:/* rem */
                val = rem32(val, val2);
                break;
            case 7:/* remu */
                val = (int32_t)remu32(val, val2);
                break;
            default:
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
1343 1344 1345 1346
        }
        else
#endif
        {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1347 1348 1349 1350 1351 1352 1353
            if (imm & ~0x20) {
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            funct3 = ((insn >> 12) & 7) | ((insn >> (30 - 3)) & (1 << 3));
            switch(funct3) {
            case 0: /* add */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1354 1355 1356 1357
#ifdef DEBUG_EXTRA
                dprintf(">>> ADD\n");
                stats[27]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1358 1359 1360
                val = (int32_t)(val + val2);
                break;
            case 0 | 8: /* sub */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1361 1362 1363 1364
#ifdef DEBUG_EXTRA
                dprintf(">>> SUB\n");
                stats[28]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1365 1366 1367
                val = (int32_t)(val - val2);
                break;
            case 1: /* sll */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1368 1369 1370 1371
#ifdef DEBUG_EXTRA
                dprintf(">>> SLL\n");
                stats[29]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1372 1373 1374
                val = (int32_t)(val << (val2 & (XLEN - 1)));
                break;
            case 2: /* slt */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1375 1376 1377 1378
#ifdef DEBUG_EXTRA
                dprintf(">>> SLT\n");
                stats[30]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1379 1380 1381
                val = (int32_t)val < (int32_t)val2;
                break;
            case 3: /* sltu */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1382 1383 1384 1385
#ifdef DEBUG_EXTRA
                dprintf(">>> SLTU\n");
                stats[31]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1386 1387 1388
                val = val < val2;
                break;
            case 4: /* xor */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1389 1390 1391 1392
#ifdef DEBUG_EXTRA
                dprintf(">>> XOR\n");
                stats[32]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1393 1394 1395
                val = val ^ val2;
                break;
            case 5: /* srl */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1396 1397 1398 1399
#ifdef DEBUG_EXTRA
                dprintf(">>> SRL\n");
                stats[33]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1400 1401 1402
                val = (int32_t)((uint32_t)val >> (val2 & (XLEN - 1)));
                break;
            case 5 | 8: /* sra */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1403 1404 1405 1406
#ifdef DEBUG_EXTRA
                dprintf(">>> SRA\n");
                stats[34]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1407 1408 1409
                val = (int32_t)val >> (val2 & (XLEN - 1));
                break;
            case 6: /* or */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1410 1411 1412 1413
#ifdef DEBUG_EXTRA
                dprintf(">>> OR\n");
                stats[35]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1414 1415 1416
                val = val | val2;
                break;
            case 7: /* and */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1417 1418 1419 1420
#ifdef DEBUG_EXTRA
                dprintf(">>> AND\n");
                stats[36]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
                val = val & val2;
                break;
            default:
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
        }
        if (rd != 0)
            reg[rd] = val;
        break;
1431 1432 1433

    case 0x73: /* SYSTEM */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1434 1435 1436 1437 1438 1439 1440 1441
        funct3 = (insn >> 12) & 7;
        imm = insn >> 20;
        if (funct3 & 4)
            val = rs1;
        else
            val = reg[rs1];
        funct3 &= 3;
        switch(funct3) {
1442

1443
        case 1: /* csrrw & csrrwi */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1444
#ifdef DEBUG_EXTRA
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
            if ((insn >> 12) & 4)
            {
               dprintf(">>> CSRRWI\n");
               stats[44]++;
            }
            else
            {
               dprintf(">>> CSRRW\n");
               stats[41]++;
            }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1455
#endif
1456
            if (csr_read(&val2, imm, TRUE)) {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            val2 = (int32_t)val2;
            err = csr_write(imm, val);
            if (err < 0) {
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            if (rd != 0)
                reg[rd] = val2;
            if (err > 0) {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1469
                /* pc = pc + 4; */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1470 1471
            }
            break;
1472

1473 1474
        case 2: /* csrrs & csrrsi */
        case 3: /* csrrc & csrrci */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1475 1476 1477 1478 1479
            if (csr_read(&val2, imm, (rs1 != 0))) {
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            val2 = (int32_t)val2;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1480
#ifdef DEBUG_EXTRA
1481 1482 1483 1484 1485 1486 1487
            switch((insn >> 12) & 7)
            {
               case 2: dprintf(">>> CSRRS\n"); stats[42]++; break;
               case 3: dprintf(">>> CSRRC\n"); stats[43]++; break;
               case 6: dprintf(">>> CSRRSI\n"); stats[45]++; break;
               case 7: dprintf(">>> CSRRCI\n"); stats[46]++; break;
            }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1488
#endif
1489 1490 1491 1492
            if (rs1 != 0)
            {
                if (funct3 == 2)
                {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1493
                    val = val2 | val;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1494
                }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1495
                else
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1496
                {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1497
                    val = val2 & ~val;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1498
                }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1499 1500 1501 1502 1503
                err = csr_write(imm, val);
                if (err < 0) {
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
1504
            }
1505 1506
            else
            {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1507 1508 1509 1510 1511
                err = 0;
            }
            if (rd != 0)
                reg[rd] = val2;
            break;
1512

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1513 1514 1515
        case 0:
            switch(imm) {
            case 0x000: /* ecall */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1516 1517 1518 1519
#ifdef DEBUG_EXTRA
                dprintf(">>> ECALL\n");
                stats[39]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1520 1521 1522 1523
                if (insn & 0x000fff80) {
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1524 1525 1526 1527
                /*
                    compliance test specific: if bit 0 of gp (x3) is 0, it is a syscall,
                    otherwise it is the program end, with the exit code in the bits 31:1
                */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1528 1529 1530 1531 1532
                if (begin_signature) {
                    if (reg[3] & 1) {
#ifdef DEBUG_OUTPUT
                        printf("program end, result: %04x\n", reg[3] >> 1);
#endif
1533
                        machine_running = FALSE;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1534
                        return;
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1535

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1536 1537 1538 1539 1540 1541 1542
                    } else {
#ifdef DEBUG_OUTPUT
                        printf("syscall: %04x\n", reg[3]);
#endif
                        raise_exception(CAUSE_USER_ECALL + priv, 0);
                    }
                } else {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1543
                    /* on real hardware, an exception is raised, the I-ECALL-01 compliance test tests this as well */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1544 1545 1546 1547 1548 1549
                    raise_exception(CAUSE_USER_ECALL + priv, 0);
                    return;
                }
                break;

            case 0x001: /* ebreak */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1550 1551 1552 1553
#ifdef DEBUG_EXTRA
                dprintf(">>> EBREAK\n");
                stats[40]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1554 1555 1556 1557 1558 1559
                if (insn & 0x000fff80) {
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
                raise_exception(CAUSE_BREAKPOINT, 0);
                return;
1560

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1561 1562
            case 0x102: /* sret */
            {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1563 1564 1565 1566
#ifdef DEBUG_EXTRA
                dprintf(">>> SRET\n");
                stats[47]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1567 1568 1569 1570 1571 1572 1573 1574
                if ((insn & 0x000fff80) || (priv < PRV_S)) {
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
                handle_sret();
                return;
            }
            break;
1575

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1576
            case 0x105: /* wfi */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1577 1578 1579 1580
#ifdef DEBUG_EXTRA
                dprintf(">>> WFI\n");
                stats[48]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1581
                /* wait for interrupt: it is allowed to execute it as nop */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1582
                break;
1583

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1584 1585
            case 0x302: /* mret */
            {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1586 1587 1588 1589
#ifdef DEBUG_EXTRA
                dprintf(">>> MRET\n");
                stats[49]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1590 1591 1592 1593 1594 1595 1596 1597
                if ((insn & 0x000fff80) || (priv < PRV_M)) {
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
                handle_mret();
                return;
            }
            break;
1598

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1599 1600
            default:
                if ((imm >> 5) == 0x09) {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1601 1602 1603 1604
#ifdef DEBUG_EXTRA
                    dprintf(">>> SFENCE.VMA\n");
                    stats[50]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
                    /* sfence.vma */
                    if ((insn & 0x00007f80) || (priv == PRV_U)) {
                        raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                        return;
                    }
                } else {
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
                break;
            }
            break;
1617

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1618 1619 1620 1621 1622
        default:
            raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
            return;
        }
        break;
1623 1624 1625

    case 0x0f: /* MISC-MEM */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1626 1627
        funct3 = (insn >> 12) & 7;
        switch(funct3) {
1628

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1629
        case 0: /* fence */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1630 1631 1632 1633
#ifdef DEBUG_EXTRA
            dprintf(">>> FENCE\n");
            stats[37]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1634 1635 1636 1637 1638
            if (insn & 0xf00fff80) {
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            break;
1639

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1640
        case 1: /* fence.i */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1641 1642 1643 1644
#ifdef DEBUG_EXTRA
            dprintf(">>> FENCE.I\n");
            stats[38]++;
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1645 1646 1647 1648 1649
            if (insn != 0x0000100f) {
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
            break;
1650

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1651 1652 1653 1654 1655
        default:
            raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
            return;
        }
        break;
1656 1657 1658 1659 1660

#ifndef STRICT_RV32I

    case 0x2f: /* AMO */

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1661 1662 1663 1664 1665 1666 1667 1668 1669
        funct3 = (insn >> 12) & 7;
        switch(funct3) {
        case 2:
        {
            uint32_t rval;

            addr = reg[rs1];
            funct3 = insn >> 27;
            switch(funct3) {
1670

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
            case 2: /* lr.w */
                if (rs2 != 0) {
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
                if (target_read_u32(&rval, addr)) {
                    raise_exception(pending_exception, pending_tval);
                    return;
                }
                val = (int32_t)rval;
                load_res = addr;
                break;
1683

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
            case 3: /* sc.w */
                if (load_res == addr) {
                    if (target_write_u32(addr, reg[rs2])) {
                        raise_exception(pending_exception, pending_tval);
                        return;
                    }
                    val = 0;
                } else {
                    val = 1;
                }
                break;
1695

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1696 1697 1698 1699 1700 1701 1702 1703 1704
            case 1: /* amiswap.w */
            case 0: /* amoadd.w */
            case 4: /* amoxor.w */
            case 0xc: /* amoand.w */
            case 0x8: /* amoor.w */
            case 0x10: /* amomin.w */
            case 0x14: /* amomax.w */
            case 0x18: /* amominu.w */
            case 0x1c: /* amomaxu.w */
1705

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
                if (target_read_u32(&rval, addr)) {
                    raise_exception(pending_exception, pending_tval);
                    return;
                }
                val = (int32_t)rval;
                val2 = reg[rs2];
                switch(funct3) {
                case 1: /* amiswap.w */
                    break;
                case 0: /* amoadd.w */
                    val2 = (int32_t)(val + val2);
                    break;
                case 4: /* amoxor.w */
                    val2 = (int32_t)(val ^ val2);
                    break;
                case 0xc: /* amoand.w */
                    val2 = (int32_t)(val & val2);
                    break;
                case 0x8: /* amoor.w */
                    val2 = (int32_t)(val | val2);
                    break;
                case 0x10: /* amomin.w */
                    if ((int32_t)val < (int32_t)val2)
                        val2 = (int32_t)val;
                    break;
                case 0x14: /* amomax.w */
                    if ((int32_t)val > (int32_t)val2)
                        val2 = (int32_t)val;
                    break;
                case 0x18: /* amominu.w */
                    if ((uint32_t)val < (uint32_t)val2)
                        val2 = (int32_t)val;
                    break;
                case 0x1c: /* amomaxu.w */
                    if ((uint32_t)val > (uint32_t)val2)
                        val2 = (int32_t)val;
                    break;
                default:
                    raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                    return;
                }
                if (target_write_u32(addr, val2)) {
                    raise_exception(pending_exception, pending_tval);
                    return;
                }
                break;
            default:
                raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
                return;
            }
        }
        break;
        default:
            raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
            return;
        }
        if (rd != 0)
            reg[rd] = val;
        break;
1765 1766 1767

#endif

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1768 1769 1770 1771 1772 1773
    default:
        raise_exception(CAUSE_ILLEGAL_INSTRUCTION, insn);
        return;
    }
}

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1774
/* returns realtime in nanoseconds */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
int64_t get_clock()
{
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return ts.tv_sec * 1000000000LL + ts.tv_nsec;
}


void riscv_cpu_interp_x32()
{
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1785
    /* we use a single execution loop to keep a simple control flow for emscripten */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1786
    while (machine_running) {
1787
#if 1
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1788
        /* update timer, assuming 10 MHz clock (100 ns period) for the mtime counter */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1789 1790
        mtime = get_clock() / 100ll;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1791
        /* for reproducible debug runs, you can use a fixed fixed increment per instruction */
1792
#else
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1793 1794 1795
        mtime += 10;
#endif
        /* default value for next PC is next instruction, can be changed by branches or exceptions */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1796 1797
        next_pc = pc + 4;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1798
        /* test for timer interrupt */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1799 1800 1801 1802 1803 1804
        if (mtimecmp <= mtime) {
            mip |= MIP_MTIP;
        }
        if ((mip & mie) != 0 && (mstatus & MSTATUS_MIE)) {
            raise_interrupt();
        } else {
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1805
            /* normal instruction execution */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1806 1807 1808 1809
            insn = get_insn32(pc);
            insn_counter++;

#ifdef DEBUG_OUTPUT
1810
            printf("[%08x]=%08x, mtime: %lx, mtimecmp: %lx\n", pc, insn, mtime, mtimecmp);
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1811 1812 1813 1814
#endif
            execute_instruction();
        }

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1815
        /* test for misaligned fetches */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1816 1817 1818 1819
        if (next_pc & 3) {
            raise_exception(CAUSE_MISALIGNED_FETCH, next_pc);
        }

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1820
        /* update current PC */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
        pc = next_pc;
    }

#ifdef DEBUG_OUTPUT
    printf("done interp %lx int=%x mstatus=%lx prv=%d\n",
           (uint64_t)insn_counter, mip & mie, (uint64_t)mstatus,
           priv);
#endif
}

int main(int argc, char** argv)
{
1833 1834 1835 1836 1837
#ifdef DEBUG_OUTPUT
    FILE *fo;
    char *po,hex_file[100];
#endif

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1838
    /* automatic STDOUT flushing, no fflush needed */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1839 1840
    setvbuf(stdout, NULL, _IONBF, 0);

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1841
    /* parse command line */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    const char* elf_file = NULL;
    const char* signature_file = NULL;
    for (int i = 1; i < argc; i++) {
        char* arg = argv[i];
        if (arg == strstr(arg, "+signature=")) {
            signature_file = arg + 11;
        } else if (arg[0] != '-') {
            elf_file = arg;
        }
    }
    if (elf_file == NULL) {
        printf("missing ELF file\n");
        return 1;
    }

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1857 1858 1859 1860
#ifdef DEBUG_EXTRA
    init_stats();
#endif

1861 1862
    for(uint32_t u=0;u<RAM_SIZE;u++) ram[u]=0;

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1863
    /* open ELF file */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1864 1865 1866 1867 1868 1869 1870 1871
    elf_version(EV_CURRENT);
    int fd = open(elf_file, O_RDONLY);
    if (fd == -1) {
        printf("can't open file %s\n", elf_file);
        return 1;
    }
    Elf *elf = elf_begin(fd, ELF_C_READ, NULL);

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1872
    /* scan for symbol table */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
    Elf_Scn *scn = NULL;
    GElf_Shdr shdr;
    while ((scn = elf_nextscn(elf, scn)) != NULL) {
        gelf_getshdr(scn, &shdr);
        if (shdr.sh_type == SHT_SYMTAB) {
            Elf_Data *data = elf_getdata(scn, NULL);
            int count = shdr.sh_size / shdr.sh_entsize;
            for (int i = 0; i < count; i++) {
                GElf_Sym sym;
                gelf_getsym(data, i, &sym);
                char* name = elf_strptr(elf, shdr.sh_link, sym.st_name);
1884 1885 1886
#if 0
                if(*name) printf("sym '%s' %lx\n",name,sym.st_value);
#endif
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1887 1888 1889 1890 1891 1892 1893
                if (strcmp(name, "begin_signature") == 0) {
                    begin_signature = sym.st_value;
                }
                if (strcmp(name, "end_signature") == 0) {
                    end_signature = sym.st_value;
                }

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1894
                /* for compliance test */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1895 1896 1897 1898
                if (strcmp(name, "_start") == 0) {
                    ram_start = sym.st_value;
                }

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1899
                /* for zephyr */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
                if (strcmp(name, "__reset") == 0) {
                    ram_start = sym.st_value;
                }
                if (strcmp(name, "__irq_wrapper") == 0) {
                    mtvec = sym.st_value;
                }
            }
        }
    }
#ifdef DEBUG_OUTPUT
    printf("begin_signature: 0x%08x\n", begin_signature);
    printf("end_signature: 0x%08x\n", end_signature);
    printf("start: 0x%08x\n", ram_start);
#endif

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1915
    /* scan for program */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1916 1917 1918 1919 1920 1921
    while ((scn = elf_nextscn(elf, scn)) != NULL) {
        gelf_getshdr(scn, &shdr);
        if (shdr.sh_type == SHT_PROGBITS) {
            Elf_Data *data = elf_getdata(scn, NULL);
            if (shdr.sh_addr >= ram_start) {
                for (size_t i = 0; i < shdr.sh_size; i++) {
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
                    ram_curr = shdr.sh_addr + i - ram_start;
                    if(ram_curr >= RAM_SIZE)
                    {
#ifdef DEBUG_OUTPUT
                        printf("memory pointer outside of range 0x%08x (section at address 0x%08x)\n", ram_curr, (uint32_t) shdr.sh_addr);
#endif
                        /* break; */
                    }
                    else
                    {
                        ram[ram_curr] = ((uint8_t *)data->d_buf)[i];
                        if(ram_curr > ram_last) ram_last = ram_curr;
                    }
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1935 1936 1937 1938 1939 1940 1941 1942 1943
                }
            } else {
#ifdef DEBUG_OUTPUT
                printf("ignoring section at address 0x%08x\n", (uint32_t) shdr.sh_addr);
#endif
            }
        }
    }

Alexander Shabarshin's avatar
Alexander Shabarshin committed
1944
    /* close ELF file */
Alexander Shabarshin's avatar
Alexander Shabarshin committed
1945 1946 1947
    elf_end(elf);
    close(fd);

1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
#ifdef DEBUG_OUTPUT
    printf("codesize: 0x%08x (%i)\n", ram_last+1, ram_last+1);
    strcpy(hex_file, elf_file);
    po = strrchr(hex_file,