diff --git a/chapter 1.tex b/chapter 1.tex index 359c8ce..8acdb1a 100644 --- a/chapter 1.tex +++ b/chapter 1.tex @@ -1,5 +1,52 @@ \chapter{Introduction} This is an attempt to distill what I know about how computers work into some kind of emulator / simulator. I'm curious to see if what I know at the moment is enough to allow me too emulate a general purpose computer. It turns out that it take quite a different skill set compared to writing web or desktop applications. But I realize that, while I understand the concepts that underpin what I do day to day, I don't have an intuitive understanding of the hardware. This has become painfully evident pretty much immediately after I started this project. An ISA should be simple, add, subtract, copy, etc., but then you have to consider the state that the processor will be in after each instruction. +\section{Conventions \& Data Types} +This machine is \quotes{little endian} meaning bytes are numbered starting from the least significant byte or the right most byte. Figure \ref{fig:bytelayout} shows how a structure is layed out in memory byte by byte. +\begin{figure}[h] + \begin{tikzpicture} + % Row 0 + \node [draw, fit={( 0.0, 0.0) ( 2.0, 0.5)}, inner sep=0, label=center:byte 0] (row0b0) {}; + \node [draw, fit={( 0.0, 0.0) (-2.0, 0.5)}, inner sep=0, label=center:byte 1] (row0b1) {}; + \node [draw, fit={(-2.0, 0.0) (-4.0, 0.5)}, inner sep=0, label=center:byte 2] (row0b2) {}; + \node [draw, fit={(-4.0, 0.0) (-6.0, 0.5)}, inner sep=0, label=center:byte 3] (row0b3) {}; + % Row 1 + \node [draw, fit={( 0.0, 0.5) (2.0, 1.0)}, inner sep=0] (row1b0) {}; + \node [draw, fit={( 0.0, 0.5) (-2.0, 1.0)}, inner sep=0] (row1b1) {}; + \node [draw, fit={(-2.0, 0.5) (-4.0, 1.0)}, inner sep=0] (row1b2) {}; + \node [draw, fit={(-4.0, 0.5) (-6.0, 1.0)}, inner sep=0] (row1b3) {}; + % Row 2 + \node [draw, fit={( 0.0, 1.0) ( 2.0, 1.5)}, inner sep=0] (row2b0) {}; + \node [draw, fit={( 0.0, 1.0) (-2.0, 1.5)}, inner sep=0] (row2b1) {}; + \node [draw, fit={(-2.0, 1.0) (-4.0, 1.5)}, inner sep=0] (row2b2) {}; + \node [draw, fit={(-4.0, 1.0) (-6.0, 1.5)}, inner sep=0] (row2b3) {}; + + % Draw Row 0 Byte Offset + \node at (row0b0.east)[right] (offset) {0}; + \node at (offset.south)[below] {Byte Offset}; + %Draw Row 1 byte offset + \node at (row1b0.east)[right] {4}; + %Draw Row 2 bit offset label and bit positions + \node at (row2b0.east)[right] {8}; % Byte offset + \node at (row2b0.north east)[above] {0}; + \node at (row2b0.north west)[above] {8 7}; + % + \node at (row2b1.north west)[above] {16 15}; + % + \node at (row2b2.north west)[above] {24 23}; + % + \node at (row2b3.north west)[above] (mostbit) {31}; + \node at (mostbit.west)[left] {Bit Offset}; + \end{tikzpicture} + \caption{Layout of a Structure in Memory} + \label{fig:bytelayout} +\end{figure} + +There are three data types that are understood by this machine: byte, short and word. +\begin{description} + \item[Byte] An octet or a structure that is exactly 8-bits wide. + \item[Short] To loosely borrow from C, a short is exactly two bytes or 16-bits wide. + \item[Word] Exactly four bytes wide, or 32-bits. +\end{description} \section{Registers} Thirty-two general purpose registers are available for program code to use however it wishes. The base name for the general purpose registers is \quotes{r} follows by an unpadded number, such as \quotes{r8}. These numbers do not follow a zero-based index scheme, \quotes{r29} is the twenty-ninth register. Using the base name implies full width, a \quotes{word}, when reading or writing from or to the register respectively. @@ -53,6 +100,7 @@ In addition to the general purpose registers there is also a base pointer (bp), \section{Memory} The memory layout is quite simple, much like many modern machines it is a linear memory map. The address bus is thirty-two bits wide, the same width of the data bus, meaning at most there is 2\textsuperscript{32} of addressable memory. Like contemporary machines the top of memory starts at address 0x00000000 and grows downward toward 0xFFFFFFFF. By default the memory is mapped as shown in Figure \ref{fig:memorylayout}. +% How memory mapped I/O works: https://superuser.com/questions/595672/how-is-memory-mapped-to-certain-hardware-how-is-mmio-accomplished-exactly \begin{figure} \begin{tikzpicture} \node [draw, fit={(0,0) (3, 6)}] (memory) {}; @@ -61,8 +109,12 @@ The memory layout is quite simple, much like many modern machines it is a linear \node (top) at (memory.north east)[right] {0x00000000}; \node at (memory.south east)[right] {0xFFFFFFFF}; - \draw[dashed] ($(memory.north west)-(0,0.5)$) node[left] {0x200000} -- ($(memory.north east)-(0,0.5)$) node[midway, above] {Firmware}; - \draw[dashed] ($(memory.north west)-(0,1.25)$) -- ($(memory.north east)-(0,1.25)$) node[right] {0x275300 (800x600)} node[midway, above] {VRAM}; + \draw[->] ($(memory.west)-(0.5,-0.25)$)[left] -- ($(memory.north west)-(0.5,0.0)$)[left] node[midway, left] {Stack}; + \draw[->] ($(memory.west)-(0.5, 0.25)$)[left] -- ($(memory.south west)-(0.5,0.0)$)[above] node[midway, left] {Heap}; + + %\draw[dashed] ($(memory.north west)-(0.0,0.5)$) node[left] {0x000800} -- ($(memory.north east)-(0.0,0.5)$) node[midway, above] {IVT - 2kb}; + %\draw[dashed] ($(memory.north west)-(0.0,1.0)$) -- ($(memory.north east)-(0.0,1.0)$) node[right] {0x200800} node[midway, above] {Firmware - 2MiB}; + %\draw[dashed] ($(memory.north west)-(0.0,1.5)$) node[left] {0x276100} -- ($(memory.north east)-(0.0,1.5)$) node[midway, above] {VRAM - 3.75MiB}; \end{tikzpicture} \caption{Memory Map \& Layout} \label{fig:memorylayout} % https://www.overleaf.com/learn/latex/Referencing_Figures