st80

Smalltalk-80 (image Xerox de 1983) dans le navigateur, VM du livre bleu en WebAssembly
git clone https://git.schurger.org/st80.git
Log | Files | Refs | README

realwordmemory.h (4222B)


      1 //
      2 //  realwordmemory.h
      3 //  Smalltalk-80
      4 //
      5 //  Created by Dan Banay on 2/25/20.
      6 //  Copyright © 2020 Dan Banay. All rights reserved.
      7 //
      8 //  MIT License
      9 //  Permission is hereby granted, free of charge, to any person obtaining a copy
     10 //  of this software and associated documentation files (the "Software"), to deal
     11 //  in the Software without restriction, including without limitation the rights
     12 //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     13 //  copies of the Software, and to permit persons to whom the Software is
     14 //  furnished to do so, subject to the following conditions:
     15 //
     16 //  The above copyright notice and this permission notice shall be included in all
     17 //  copies or substantial portions of the Software.
     18 //
     19 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20 //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21 //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     22 //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     23 //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     24 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     25 //  SOFTWARE.
     26 //
     27 
     28 
     29 #pragma once
     30 
     31 #include <cstdint>
     32 #include <cassert>
     33 
     34 // Segmented Memory Model as described in G&R pg. 656
     35 class RealWordMemory
     36 {
     37 public:
     38     static const int SegmentCount = 16;
     39     static const int SegmentSize = 65536; /* in words */
     40     
     41     RealWordMemory()
     42     {
     43     }
     44     
     45     inline int segment_word(int s, int w)
     46     {
     47         assert(s >= 0 && s < SegmentCount);
     48         assert(w >= 0 && w < SegmentSize);
     49         return memory[s][w];
     50     }
     51     
     52     inline int segment_word_put(int s, int w, int value)
     53     {
     54         assert(s >= 0 && s < SegmentCount);
     55         assert(w >= 0 && w < SegmentSize);
     56         assert(value >= 0 && value < 65536);
     57         memory[s][w] = value;
     58         return value;
     59     }
     60     
     61     inline int segment_word_byte(int s, int w, int byteNumber)
     62     {
     63         assert(s >= 0 && s < SegmentCount);
     64         assert(w >= 0 && w < SegmentSize);
     65         assert(byteNumber == 1 || byteNumber == 0);
     66         
     67         // byteNumber 0 is the byte of the word that appears first in member, 1 is the next
     68         /* big endian */
     69         //return (memory[s][w] >> (8*byteNumber)) & 0xff;
     70         return  ((std::uint8_t *) &memory[s][w])[byteNumber];
     71         /* little endian */
     72         //return  ((std::uint8_t *) &memory[s][w])[1-byteNumber];
     73         
     74     }
     75     
     76     inline int segment_word_byte_put(int s, int w, int byteNumber, int value)
     77     {
     78         assert(s >= 0 && s < SegmentCount);
     79         assert(w >= 0 && w < SegmentSize);
     80         assert(value >= 0 && value < 65536);
     81         assert(byteNumber == 1 || byteNumber == 0);
     82         
     83         // byteNumber 0 is the byte of the word that appears first in member, 1 is the next
     84         /* big endian */
     85         ((std::uint8_t *) &memory[s][w])[byteNumber] = value;
     86         /* little endian */
     87         //((std::uint8_t *) &memory[s][w])[1-byteNumber] = value;
     88         
     89         return value;
     90     }
     91     
     92     // The most significant bit in a word will be referred to with the index 0 and the least significant with the index 15. G&R 657
     93     inline int segment_word_bits_to(int s, int w, int firstBitIndex, int lastBitIndex)
     94     {
     95         assert(s >= 0 && s < SegmentCount);
     96         assert(w >= 0 && w < SegmentSize);
     97         
     98         std::uint16_t shift = memory[s][w] >> (15-lastBitIndex);
     99         std::uint16_t mask = (1 << (lastBitIndex - firstBitIndex + 1)) - 1;
    100         
    101         return shift & mask;
    102     }
    103     
    104     inline int segment_word_bits_to_put(int s, int w, int firstBitIndex, int lastBitIndex, int value)
    105     {
    106         assert(s >= 0 && s < SegmentCount);
    107         assert(w >= 0 && w < SegmentSize);
    108         assert(value >= 0 && value < 65536);
    109         
    110         std::uint16_t mask = (1 << (lastBitIndex - firstBitIndex + 1)) - 1;
    111         assert((value & mask) == value); // make sure it fits
    112         memory[s][w] = (memory[s][w] & ~(mask << (15-lastBitIndex))) | (value << (15 - lastBitIndex));
    113         return value;
    114     }
    115     
    116 private:
    117     std::uint16_t memory[SegmentCount][SegmentSize];
    118     
    119 };
    120