1
        2
        3
        4
        5
        6
        7
        8
        9
       10
       11
       12
       13
       14
       15
       16
       17
       18
       19
       20
       21
       22
       23
       24
       25
       26
       27
       28
       29
       30
       31
       32
       33
       34
       35
       36
       37
       38
       39
       40
       41
       42
       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
       87
       88
       89
       90
       91
       92
       93
       94
       95
       96
       97
       98
       99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      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
      198
      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
      301
      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
      333
      334
      335
      336
      337
      338
      339
      340
      341
      342
      343
      344
      345
      346
      347
      348
      349
      350
      351
      352
      353
      354
      355
      356
      357
      358
      359
      360
      361
      362
      363
      364
      365
      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
      462
      463
      464
      465
      466
      467
      468
      469
      470
      471
      472
      473
      474
      475
      476
      477
      478
      479
      480
      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
      512
      513
      514
      515
      516
      517
      518
      519
      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
      562
      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
      599
      600
      601
      602
      603
      604
      605
      606
      607
      608
      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
      687
      688
      689
      690
      691
      692
      693
      694
      695
      696
      697
      698
      699
      700
      701
      702
      703
      704
      705
      706
      707
      708
      709
      710
      711
      712
      713
      714
      715
      716
      717
      718
      719
      720
      721
      722
      723
      724
      725
      726
      727
      728
      729
      730
      731
      732
      733
      734
      735
      736
      737
      738
      739
      740
      741
      742
      743
      744
      745
      746
      747
      748
      749
      750
      751
      752
      753
      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
      781
      782
      783
      784
      785
      786
      787
      788
      789
      790
      791
      792
      793
      794
      795
      796
      797
      798
      799
      800
      801
      802
      803
      804
      805
      806
      807
      808
      809
      810
      811
      812
      813
      814
      815
      816
      817
      818
      819
      820
      821
      822
      823
      824
      825
      826
      827
      828
      829
      830
      831
      832
      833
      834
      835
      836
      837
      838
      839
      840
      841
      842
      843
      844
      845
      846
      847
      848
      849
      850
      851
      852
      853
      854
      855
      856
      857
      858
      859
      860
      861
      862
      863
      864
      865
      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
      921
      922
      923
      924
      925
      926
      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
      962
      963
      964
      965
      966
      967
      968
      969
      970
      971
      972
      973
      974
      975
      976
      977
      978
      979
      980
      981
      982
      983
      984
      985
      986
      987
      988
      989
      990
      991
      992
      993
      994
      995
      996
      997
      998
      999
     1000
     1001
     1002
     1003
     1004
     1005
     1006
     1007
     1008
     1009
     1010
     1011
     1012
     1013
     1014
     1015
     1016
     1017
     1018
     1019
     1020
     1021
     1022
     1023
     1024
     1025
     1026
     1027
     1028
     1029
     1030
     1031
     1032
     1033
     1034
     1035
     1036
     1037
     1038
     1039
     1040
     1041
     1042
     1043
     1044
     1045
     1046
     1047
     1048
     1049
     1050
     1051
     1052
     1053
     1054
     1055
     1056
     1057
     1058
     1059
     1060
     1061
     1062
     1063
     1064
     1065
     1066
     1067
     1068
     1069
     1070
     1071
     1072
     1073
     1074
     1075
     1076
     1077
     1078
     1079
     1080
     1081
     1082
     1083
     1084
     1085
     1086
     1087
     1088
     1089
     1090
     1091
     1092
     1093
     1094
     1095
     1096
     1097
     1098
     1099
     1100
     1101
     1102
     1103
     1104
#define __STDC_FORMAT_MACROS
#include <algorithm>
#include <cstring>
#include <cinttypes>
#include <iostream>
#include <cassert>
#include <cstdio>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <queue>
#include <vector>
#include <random>

#include <iostream>

struct RAM
{
    RAM()
    {
    }

    char
    operator()(uint64_t address) const
    {
        if (!data.count(address)) {
            std::random_device rd;
            std::mt19937_64 gen(rd());
            std::uniform_int_distribution<char> dis;

            data[address] = dis(gen);
        }
        return data[address];
    }

    char &
    operator()(uint64_t address)
    {
        if (!data.count(address)) {
            std::random_device rd;
            std::mt19937_64 gen(rd());
            std::uniform_int_distribution<char> dis;

            data[address] = dis(gen);
        }
        return data[address];
    }

    void
    print(uint64_t fromAddr, uint64_t toAddr) const
    {
        for (uint64_t from=fromAddr;
             from<toAddr;
             (from+40>from) ? from+=40 : from=toAddr)
        {
            uint64_t to = (from+40>from) ? std::min(from+40, toAddr)
                                         : toAddr;

            for (uint64_t i=from; i<=to && (i-1<i || from==0); ++i) {
                std::printf("---");
            }
            std::printf("\n");
            for (uint64_t i=from; i<=to && (i-1<i || from==0); ++i) {
                std::printf("%02x ", 0xFF & operator()(i));
            }
            std::printf("\n");
            for (uint64_t i=from; i<=to && (i-1<i || from==0); ++i) {
                std::printf("---");
            }
            std::printf("\n");
            for (uint64_t i=from; i<=to && (i-1<i || from==0); ++i) {
                if (i % 8 == 0) {
                    std::printf("|%-23" PRIu64, i);
                }
            }
            std::printf("\n");
            for (uint64_t i=from; i<=to && (i-1<i || from==0); ++i) {
                if (i % 8 == 0) {
                    std::printf("|0x%-21" PRIx64, i);
                }
            }
            std::printf("\n\n");
        }
    }

    mutable std::unordered_map<uint64_t,char>  data;
};

struct CpuRegister
{
    CpuRegister()
        : zero(0)
    {
        std::random_device rd;
        std::mt19937_64 gen(rd());
        std::uniform_int_distribution<uint64_t> dis;

        for (int i=0; i<256; ++i) {

            r[i] = dis(gen);
        }
    }


    const uint64_t &
    operator()(unsigned char id) const
    {
        zero = 0;
        if (id==0) {
            return zero;
        }
        return r[id];
    }

    uint64_t &
    operator()(unsigned char id)
    {
        zero = 0;
        return (id!=0) ? r[id] : zero;
    }

    void
    print(unsigned char     from,
          unsigned char     to) const
    {

        for (unsigned char i=from; i<=to; ++i) {
            std::printf("Reg%-2d   0x%016" PRIx64, i, operator()(i));
            if (i==0) {
                std::printf(" (Zero register)");
            }
            std::printf("\n");
        }
        std::printf("\n\n");
    }

    uint64_t  r[256];
    mutable uint64_t  zero;
};

struct IO
{
    char
    get()
    {
        std::printf("Input one character: ");
        char c = getchar();
        getchar();
        return c;
    }

    void
    put(char c)
    {
        output.append(1, c);
    }

    void
    print()
    {
        std::printf("Terminal:\n");
        if (output.length()!=0) {
            std::printf("%s\n\n", output.c_str());
        }
    }

    std::string         output;
};

struct DataBus
{
    DataBus(RAM &ram, CpuRegister &cpuRegister, IO &io)
        : ram(ram), cpuRegister(cpuRegister), io(io)
    {
    }

    template <unsigned int bytes, bool asSigned=true>
    void
    fetch(uint64_t address, unsigned char reg)
    {
        assert(bytes==1 || bytes==2 || bytes==4 || bytes==8);
        assert(address % bytes == 0);

        cpuRegister(reg) = 0;
        if (asSigned && (0x80 & ram(address))) {
            cpuRegister(reg) = -1;
        }
        for (unsigned int i=0; i<bytes; ++i) {
            cpuRegister(reg) = cpuRegister(reg) << 8;
            cpuRegister(reg) = cpuRegister(reg) | (0xFF & ram(address+i));
        }
    }

    std::uint32_t
    fetchInstr(uint64_t address)
    {
        assert(address % 4 == 0);

        std::uint32_t ir = 0;
        for (unsigned int i=0; i<4; ++i) {
            ir = ir  << 8;
            ir = ir | (0xFF & ram(address+i));
        }
        return ir;
    }

    template <unsigned int bytes>
    void
    store(unsigned char reg, uint64_t address)
    {
        assert(bytes==1 || bytes==2 || bytes==4 || bytes==8);
        assert(address % bytes == 0);

        uint64_t value = cpuRegister(reg);
        for (unsigned int i=0; i<bytes; ++i) {
            char byte      = value & 0xFF;
            ram(address+bytes-i-1) = byte;
            value = value >> 8;
        }
    }

    template <unsigned int bytes>
    void
    store(uint64_t value, uint64_t address)
    {
        assert(bytes==1 || bytes==2 || bytes==4 || bytes==8);
        assert(address % bytes == 0);

        for (unsigned int i=0; i<bytes; ++i) {
            char byte      = value & 0xFF;
            ram(address+bytes-i-1) = byte;
            value = value >> 8;
        }
    }

    void
    get(unsigned char reg)
    {
        unsigned char c = io.get();
        cpuRegister(reg) = c;
    }

    void
    put(unsigned char reg)
    {
        unsigned char c = (0xFF & cpuRegister(reg));
        io.put(c);
    }

    void
    putc(unsigned char c)
    {
        io.put(c);
    }


    RAM         &ram;
    CpuRegister &cpuRegister;
    IO          &io;
};


struct ALU
{
    ALU(CpuRegister &cpuRegister)
        : cpuRegister(cpuRegister), zf(0), of(0), sf(0), cf(0)
    {
    }

    void
    setWord(uint16_t valueXY, int regZ)
    {
        cpuRegister(regZ) = valueXY;
    }

    void
    bitwiseOR(unsigned char regX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueX = cpuRegister(regX);
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = valueX | valueY;
        zf = cpuRegister(regZ)==0 ? 1 : 0;
    }

    void
    bitwiseOR(uint64_t valueX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = valueX | valueY;
        zf = cpuRegister(regZ)==0 ? 1 : 0;
    }

    void
    bitwiseAND(unsigned char regX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueX = cpuRegister(regX);
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = valueX & valueY;
        zf = cpuRegister(regZ)==0 ? 1 : 0;
    }

    void
    bitwiseAND(uint64_t valueX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = valueX & valueY;
        zf = cpuRegister(regZ)==0 ? 1 : 0;
    }

    void
    bitwiseSHL(unsigned char regX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueX = cpuRegister(regX);
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = (valueY << valueX);
    }

    void
    bitwiseSHL(uint64_t valueX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = (valueY << valueX);
    }

    void
    bitwiseSHR(unsigned char regX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueX = cpuRegister(regX);
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = (valueY >> valueX);
    }

    void
    bitwiseSHR(uint64_t valueX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueY = cpuRegister(regY);

        cpuRegister(regZ) = (valueY >> valueX);
    }

    void
    add(unsigned char regX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueX = cpuRegister(regX);
        uint64_t valueY = cpuRegister(regY);
        uint64_t valueZ = cpuRegister(regZ);

        zf = of = sf = cf = 0;

        valueZ = valueX + valueY;
        if ((valueZ < valueX) || (valueZ < valueY)) {
            cf = 1;
        }
        if (valueZ==0) {
            zf = 1;
        }

        bool signX = (valueX>>63);
        bool signY = (valueY>>63);
        bool signZ = (valueZ>>63);

        if ((signX==signY) && (signX!=signZ)) {
            of = 1;
        }

        sf = signZ;
        cpuRegister(regZ) = valueZ;
    }

    void
    add(uint64_t valueX, unsigned char regY, unsigned char regZ)
    {
        uint64_t valueY = cpuRegister(regY);
        uint64_t valueZ = cpuRegister(regZ);

        zf = of = sf = cf = 0;

        valueZ = valueX + valueY;
        if ((valueZ < valueX) || (valueZ < valueY)) {
            cf = 1;
        }
        if (valueZ==0) {
            zf = 1;
        }

        bool signX = (valueX>>63);
        bool signY = (valueY>>63);
        bool signZ = (valueZ>>63);

        if ((signX==signY) && (signX!=signZ)) {
            of = 1;
        }

        sf = signZ;
        cpuRegister(regZ) = valueZ;
    }

    void
    sub(unsigned char regX, unsigned char regY, unsigned char regZ)
    {
        add(-cpuRegister(regX), regY, regZ);

        uint64_t valueX = cpuRegister(regX);
        uint64_t valueY = cpuRegister(regY);
        uint64_t valueZ = cpuRegister(regZ);

        valueZ = valueY - valueX;

        if (valueZ>valueY) {
            cf = 1;
        } else {
            cf = 0;
        }
    }

    void
    sub(uint64_t valueX, unsigned char regY, unsigned char regZ)
    {
        add(-valueX, regY, regZ);

        uint64_t valueY = cpuRegister(regY);
        uint64_t valueZ = cpuRegister(regZ);

        valueZ = valueY - valueX;

        if (valueZ>valueY) {
            cf = 1;
        } else {
            cf = 0;
        }
    }

    void
    unsignedMul(unsigned char regX, unsigned char regY, int regZ)
    {
        cpuRegister(regZ) = cpuRegister(regX) * cpuRegister(regY);
    }

    void
    unsignedMul(uint64_t valueX, unsigned char regY, int regZ)
    {
        cpuRegister(regZ) = valueX * cpuRegister(regY);
    }

    void
    print()
    {
        std::printf("ZF = %1d, OF = %1d, SF = %1d, CF = %1d\n", zf, of, sf, cf);
    }

    CpuRegister &cpuRegister;

    int         zf, of, sf, cf;
};

struct CPU
{
    CPU(RAM &ram, IO &io)
        : alu(cpuRegister), dataBus(ram, cpuRegister, io), exit(0)
    {
        ip     = 0;         // Instruction Pointer (IP)
        ir     = 0;         // Instruction Register (IR)
        IR_asm = "halt";
    }

    void
    print()
    {
        std::printf("IP      0x%016" PRIx64 " (= %" PRIu64 ")\n", ip, ip);
        std::printf("IR:     0x%08" PRIx32 "         (ASM: %-s)\n\n",
                    ir, IR_asm.c_str());
        cpuRegister.print(0,15);
        alu.print();
    }

    bool
    cycle()
    {
        char asmBuffer[101];
        bool run = true;


        // (A) FETCH
        ir = dataBus.fetchInstr(ip);

        // (B) DECODE
        uint32_t instruction = ir;

        unsigned char     Z           = instruction       & 0xFF;
        unsigned char     Y           = (instruction>> 8) & 0xFF;
        unsigned char     X           = (instruction>>16) & 0xFF;
        unsigned char     op          = (instruction>>24) & 0xFF;

        signed char Xs = X;
        signed char Ys = Y;
        signed char Zs = Z;

        // (C) EXECUTE
        uint64_t IP_old = ip;

        uint16_t XY = X;
        XY = (XY<<8) | Y;

        uint32_t XYZ = XY;
        XYZ = (XYZ<<8) | Z;

        int32_t XYZs = (int16_t)XY;
        XYZs = (XYZs<<8) | Z;


        switch (op) {
            // halt
            case 0x00:
                std::snprintf(asmBuffer, 100, "halt %%%d", X);
                run = false;
                exit = cpuRegister(X);
                break;

            // get
            case 0x01:
                std::snprintf(asmBuffer, 100,
                              "get %%%d", X);
                dataBus.get(X);
                break;

            // put
            case 0x02:
                std::snprintf(asmBuffer, 100,
                              "put %%%d", X);
                dataBus.put(X);
                break;

            // put
            case 0x03:
                std::snprintf(asmBuffer, 100,
                              "put $%d, $%d, $%d", X, Y, Z);
                dataBus.putc(X);
                dataBus.putc(Y);
                dataBus.putc(Z);
                break;

            // NOP / illegal instruction
            case 0xFF:
                std::snprintf(asmBuffer, 100, "nop");
                break;

            // load $XY, %Z
            case 0xF6:
                std::snprintf(asmBuffer, 100,
                              "load $%d, %%%d", XY, Z);
                alu.setWord(XY, Z);
                break;


            //
            // Data Bus:  Fetch
            //

            // movq (%X,%Y), %Z
            case 0x10:
                std::snprintf(asmBuffer, 100,
                              "movq (%%%d, %%%d), %%%d", X, Y, Z);
                dataBus.fetch<8>(cpuRegister(X)+cpuRegister(Y), Z);
                break;

            // movq Xs(%Y), %Z
            case 0x11:
                std::snprintf(asmBuffer, 100,
                              "movq %d(%%%d), %%%d", Xs, Y, Z);
                dataBus.fetch<8>(Xs+cpuRegister(Y), Z);
                break;

            // movzlq (%X,%Y), %Z
            case 0x12:
                std::snprintf(asmBuffer, 100,
                              "movzlq (%%%d, %%%d), %%%d", X, Y, Z);
                dataBus.fetch<4, false>(cpuRegister(X)+cpuRegister(Y), Z);
                break;

            // movslq (%X,%Y), %Z
            case 0x13:
                std::snprintf(asmBuffer, 100,
                              "movslq (%%%d, %%%d), %%%d", X, Y, Z);
                dataBus.fetch<4, true>(cpuRegister(X)+cpuRegister(Y), Z);
                break;

            // movzlq Y(%X), %Z
            case 0x14:
                std::snprintf(asmBuffer, 100,
                              "movzlq %d(%%%d), %%%d", Ys, X, Z);
                dataBus.fetch<4, false>(cpuRegister(X)+Ys, Z);
                break;

            // movslq Y(%X), %Z
            case 0x15:
                std::snprintf(asmBuffer, 100,
                              "movslq %d(%%%d), %%%d", Ys, X, Z);
                dataBus.fetch<4, true>(cpuRegister(X)+Ys, Z);
                break;

            // movzwq (%X,%Y), %Z
            case 0x16:
                std::snprintf(asmBuffer, 100,
                              "movzwq (%%%d, %%%d), %%%d", X, Y, Z);
                dataBus.fetch<2, false>(cpuRegister(X)+cpuRegister(Y), Z);
                break;

            // movswq (%X,%Y), %Z
            case 0x17:
                std::snprintf(asmBuffer, 100,
                              "movswq (%%%d, %%%d), %%%d", X, Y, Z);
                dataBus.fetch<2, true>(cpuRegister(X)+cpuRegister(Y), Z);
                break;

            // movzwq Y(%X), %Z
            case 0x18:
                std::snprintf(asmBuffer, 100,
                              "movzwq %d(%%%d), %%%d", Ys, X, Z);
                dataBus.fetch<2, false>(cpuRegister(X)+Ys, Z);
                break;

            // movswq Y(%X), %Z
            case 0x19:
                std::snprintf(asmBuffer, 100,
                              "movswq %d(%%%d), %%%d", Ys, X, Z);
                dataBus.fetch<2, true>(cpuRegister(X)+Ys, Z);
                break;

            // movzbq (%X,%Y), %Z
            case 0x1A:
                std::snprintf(asmBuffer, 100,
                              "movzbq (%%%d, %%%d), %%%d", X, Y, Z);
                dataBus.fetch<1, false>(cpuRegister(X)+cpuRegister(Y), Z);
                break;

            // movsbq (%X,%Y), %Z
            case 0x1B:
                std::snprintf(asmBuffer, 100,
                              "movsbq (%%%d, %%%d), %%%d", X, Y, Z);
                dataBus.fetch<1, true>(cpuRegister(X)+cpuRegister(Y), Z);
                break;

            // movzbq Y(%X), %Z
            case 0x1C:
                std::snprintf(asmBuffer, 100,
                              "movzbq %d(%%%d), %%%d", Ys, X, Z);
                dataBus.fetch<1, false>(cpuRegister(X)+Ys, Z);
                break;

            // movsbq Y(%X), %Z
            case 0x1D:
                std::snprintf(asmBuffer, 100,
                              "movsbq %d(%%%d), %%%d", Ys, X, Z);
                dataBus.fetch<1, true>(cpuRegister(X)+Ys, Z);
                break;


            //
            // Data Bus:  Store
            //

            // movq %X, (%Y,%Z)
            case 0x40:
                std::snprintf(asmBuffer, 100,
                              "movq %%%d, (%%%d, %%%d)", X, Y, Z);
                dataBus.store<8>(X, cpuRegister(Y)+cpuRegister(Z));
                break;

            // movq %X, Z(%Y)
            case 0x41:
                std::snprintf(asmBuffer, 100,
                              "movq %%%d, %d(%%%d)", X, Zs, Y);
                dataBus.store<8>(X, cpuRegister(Y)+Zs);
                break;

            // movl %X, (%Y,%Z)
            case 0x42:
                std::snprintf(asmBuffer, 100,
                              "movl %%%d, (%%%d, %%%d)", X, Y, Z);
                dataBus.store<4>(X, cpuRegister(Y)+cpuRegister(Z));
                break;

            // movl %X, Z(%Y)
            case 0x43:
                std::snprintf(asmBuffer, 100,
                              "movl %%%d, %d(%%%d)", X, Zs, Y);
                dataBus.store<4>(X, cpuRegister(Y)+Zs);
                break;

            // movw %X, (%Y,%Z)
            case 0x44:
                std::snprintf(asmBuffer, 100,
                              "movw %%%d, (%%%d, %%%d)", X, Y, Z);
                dataBus.store<2>(X, cpuRegister(Y)+cpuRegister(Z));
                break;

            // movw %X, Z(%Y)
            case 0x45:
                std::snprintf(asmBuffer, 100,
                              "movw %%%d, %d(%%%d)", X, Zs, Y);
                dataBus.store<2>(X, cpuRegister(Y)+Zs);
                break;


            // movb %X, (%Y,%Z)
            case 0x46:
                std::snprintf(asmBuffer, 100,
                              "movb %%%d, (%%%d, %%%d)", X, Y, Z);
                dataBus.store<1>(X, cpuRegister(Y)+cpuRegister(Z));
                break;

            // movb %X, Z(%Y)
            case 0x47:
                std::snprintf(asmBuffer, 100,
                              "movb %%%d, %d(%%%d)", X, Zs, Y);
                dataBus.store<1>(X, cpuRegister(Y)+Zs);
                break;


            //
            //  Integer-Arithmetic
            //

            // addq %X, %Y, %Z
            case 0x60:
                std::snprintf(asmBuffer, 100,
                              "addq  %%%d, %%%d, %%%d", X, Y, Z);
                alu.add(X, Y, Z);
                break;

            // addq $X, %Y, %Z
            case 0x61:
                std::snprintf(asmBuffer, 100,
                              "addq  $%d, %%%d, %%%d", Xs, Y, Z);
                alu.add(uint64_t(Xs), Y, Z);
                break;

            // subq %X, %Y, %Z
            case 0x62:
                std::snprintf(asmBuffer, 100,
                              "subq  %%%d, %%%d, %%%d", X, Y, Z);
                alu.sub(X, Y, Z);
                break;

            // subq $X, %Y, %Z
            case 0x63:
                std::snprintf(asmBuffer, 100,
                              "subq  $%d, %%%d, %%%d", Xs, Y, Z);
                alu.sub(uint64_t(Xs), Y, Z);
                break;

            // mulq %X, %Y, %Z
            case 0x70:
                std::snprintf(asmBuffer, 100,
                              "mulq  %%%d, %%%d, %%%d", X, Y, Z);
                alu.unsignedMul(X, Y, Z);
                break;


            // mulq $X, %Y, %Z
            case 0x71:
                std::snprintf(asmBuffer, 100,
                              "mulq  $%d, %%%d, %%%d", X, Y, Z);
                alu.unsignedMul(uint64_t(X), Y, Z);
                break;

            //
            // Bit-Operations
            //

            // orq %X, %Y, %Z
            case 0x80:
                std::snprintf(asmBuffer, 100,
                              "orq   %%%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseOR(X, Y, Z);
                break;

            // orq $X, %Y, %Z
            case 0x81:
                std::snprintf(asmBuffer, 100,
                              "orq   $%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseOR(uint64_t(X), Y, Z);
                break;

            // andq %X, %Y, %Z
            case 0x82:
                std::snprintf(asmBuffer, 100,
                              "andq  %%%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseAND(X, Y, Z);
                break;

            // andq $X, %Y, %Z
            case 0x83:
                std::snprintf(asmBuffer, 100,
                              "andq  $%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseAND(uint64_t(X), Y, Z);
                break;

            // shlq %X, %Y, %Z
            case 0x84:
                std::snprintf(asmBuffer, 100,
                              "shlq  %%%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseSHL(X, Y, Z);
                break;

            // shlq $X, %Y, %Z
            case 0x85:
                std::snprintf(asmBuffer, 100,
                              "shlq  $%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseSHL(uint64_t(X), Y, Z);
                break;

            // shrq %X, %Y, %Z
            case 0x86:
                std::snprintf(asmBuffer, 100,
                              "shrq  %%%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseSHR(X, Y, Z);
                break;

            // shrq $X, %Y, %Z
            case 0x87:
                std::snprintf(asmBuffer, 100,
                              "shrq  $%d, %%%d, %%%d", X, Y, Z);
                alu.bitwiseSHR(uint64_t(X), Y, Z);
                break;

            // jmp $XYZ
            case 0x9A:
                std::snprintf(asmBuffer, 100,
                              "jmp   @+$%" PRId32, XYZs);
                ip = int64_t(ip)+ 4*int64_t(XYZs);
                break;

            // jmp %X, %Y
            case 0x9B:
                std::snprintf(asmBuffer, 100,
                              "jmp   %%%d, %%%d", X, Y);
                cpuRegister(Y) = ip + 4;
                ip             = cpuRegister(X);
                break;

            // jmp %X
            case 0x9C:
                std::snprintf(asmBuffer, 100,
                              "jmp   %%%d", X);
                --cpuRegister(Y);
                ip = cpuRegister(X+cpuRegister(Y));
                break;

            // jz $XYZ
            case 0x90:
                std::snprintf(asmBuffer, 100,
                              "jz    @+$%d (false)", XYZs);
                if (alu.zf) {
                    std::snprintf(asmBuffer, 100,
                                  "jz    @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // jnz $XYZ
            case 0x91:
                std::snprintf(asmBuffer, 100,
                              "jnz   @+$%d (false)", XYZs);
                if (!alu.zf) {
                    std::snprintf(asmBuffer, 100,
                                  "jnz   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // jl $XYZ
            case 0x92:
                std::snprintf(asmBuffer, 100,
                              "jl   @+$%d (false)", XYZs);
                if (alu.sf != alu.of) {
                    std::snprintf(asmBuffer, 100,
                                  "jl   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // jge $XYZ
            case 0x93:
                std::snprintf(asmBuffer, 100,
                              "jge   @+$%d (false)", XYZs);
                if (alu.sf == alu.of) {
                    std::snprintf(asmBuffer, 100,
                                  "jge   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;


            // jle $XYZ
            case 0x94:
                std::snprintf(asmBuffer, 100,
                              "jle   @+$%d (false)", XYZs);
                if (alu.zf || (alu.sf != alu.of)) {
                    std::snprintf(asmBuffer, 100,
                                  "jle   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // jg $XYZ
            case 0x95:
                std::snprintf(asmBuffer, 100,
                              "jg   @+$%d (false)", XYZs);
                if ((alu.zf==0) && (alu.sf == alu.of)) {
                    std::snprintf(asmBuffer, 100,
                                  "jg   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // jb $XYZ
            case 0x96:
                std::snprintf(asmBuffer, 100,
                              "jle   @+$%d (false)", XYZs);
                if (alu.cf) {
                    std::snprintf(asmBuffer, 100,
                                  "jle   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // jnb $XYZ
            // jae $XYZ
            case 0x97:
                std::snprintf(asmBuffer, 100,
                              "jnb   @+$%d (false)", XYZs);
                if (!alu.cf) {
                    std::snprintf(asmBuffer, 100,
                                  "jnb   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // jbe $XYZ
            case 0x98:
                std::snprintf(asmBuffer, 100,
                              "jbe   @+$%d (false)", XYZs);
                std::printf("%d %d\n", alu.cf, alu.zf);
                if (alu.cf || alu.zf) {
                    std::snprintf(asmBuffer, 100,
                                  "jbe   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            // ja $XYZ
            case 0x99:
                std::snprintf(asmBuffer, 100,
                              "ja   @+$%d (false)", XYZs);
                if (!alu.cf && !alu.zf) {
                    std::snprintf(asmBuffer, 100,
                                  "ja   @+$%d (true)", XYZs);
                    ip = int64_t(ip)+ 4*int64_t(XYZs);
                }
                break;

            default:
                std::fprintf(stderr, "Illegal instruction or instruction not "
                                     "implemented\n");
                assert(0);
        }

        if (IP_old==ip) {
            ip += 4;
        }

        // (D) STORE


        IR_asm = std::string(asmBuffer);
        return run;
    }

    CpuRegister cpuRegister;
    uint32_t    ir;
    uint64_t    ip;
    ALU         alu;
    DataBus     dataBus;
    int         exit;
    std::string IR_asm;
};

struct Computer
{
    Computer(const char *filename, bool interactive=true)
        : cpu(ram, io), cycle(0), exit(0), interactive(interactive)
    {
        if (!load(filename)) {
            std::fprintf(stderr, "Code in %s corrupted\n", filename);
            assert(0);
        }
    }

    void
    run()
    {
        do {
            print();
            ++cycle;

            if (interactive) {
                std::getchar();
            }
        } while (cpu.cycle());
        print();
    }

    bool
    load(const char *filename)
    {
        std::ifstream   infile(filename);
        std::string     line;
        std::uint64_t   code;
        std::uint64_t   addr = 0;

        while (std::getline(infile, line))
        {
            line.erase(find(line.begin(), line.end(), '#'),
                       line.end());
            line.erase(remove_if(line.begin(), line.end(), isspace),
                       line.end());
            std::istringstream in(line);

            size_t len = in.str().length();

            if (len==0) {
                continue;
            }

            if (len % 2) {
                return false;
            }

            if (! (in >> std::hex >> code)) {
                return false;
            }

            addr += len/2;
            for (size_t i=0; i<len/2; ++i) {
                ram(addr-i-1) = 0xFF & (code >> i*8);
            }

        }
        return true;
    }

    void
    print()
    {
        std::printf("CPU cycles done %4d\n\n", cycle);
        ram.print(0,160);
        std::printf("....\n\n");
        ram.print(uint64_t(-1)-79,uint64_t(-1));
        cpu.print();
        io.print();
        std::printf("\n\n");
    }

    RAM         ram;
    IO          io;
    CPU         cpu;
    int         cycle;
    int         exit;
    bool        interactive;
};

int
main(int argc, const char **argv)
{
    const char *code;
    bool       interactive = true;

    if (argc<2) {
        std::fprintf(stderr, "usage: [-r] %s code-file\n", argv[0]);
        return 1;
    }
    for (int i=1; i<argc; ++i) {
        if (!strcmp(argv[i], "-r")) {
            interactive = false;
        } else {
            code = argv[i];
        }
    }

    Computer    computer(code, interactive);

    computer.run();
    std::printf("ULM was halted with exit code %d (=0x%02X)\n",
                computer.cpu.exit, computer.cpu.exit);
    return computer.cpu.exit;
}