1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
6 <!-- 2023-09-18 Mon 01:46 -->
7 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
8 <meta name="viewport" content="width=device-width, initial-scale=1" />
9 <title>Fifth - virtual machine</title>
10 <meta name="author" content="Svjatoslav Agejenko" />
11 <meta name="generator" content="Org Mode" />
13 #content { max-width: 60em; margin: auto; }
14 .title { text-align: center;
15 margin-bottom: .2em; }
16 .subtitle { text-align: center;
20 .todo { font-family: monospace; color: red; }
21 .done { font-family: monospace; color: green; }
22 .priority { font-family: monospace; color: orange; }
23 .tag { background-color: #eee; font-family: monospace;
24 padding: 2px; font-size: 80%; font-weight: normal; }
25 .timestamp { color: #bebebe; }
26 .timestamp-kwd { color: #5f9ea0; }
27 .org-right { margin-left: auto; margin-right: 0px; text-align: right; }
28 .org-left { margin-left: 0px; margin-right: auto; text-align: left; }
29 .org-center { margin-left: auto; margin-right: auto; text-align: center; }
30 .underline { text-decoration: underline; }
31 #postamble p, #preamble p { font-size: 90%; margin: .2em; }
32 p.verse { margin-left: 3%; }
34 border: 1px solid #e6e6e6;
36 background-color: #f2f2f2;
38 font-family: monospace;
53 background-color: #f2f2f299;
55 pre.src:hover:before { display: inline; margin-top: 14px;}
56 /* Languages per Org manual */
57 pre.src-asymptote:before { content: 'Asymptote'; }
58 pre.src-awk:before { content: 'Awk'; }
59 pre.src-authinfo::before { content: 'Authinfo'; }
60 pre.src-C:before { content: 'C'; }
61 /* pre.src-C++ doesn't work in CSS */
62 pre.src-clojure:before { content: 'Clojure'; }
63 pre.src-css:before { content: 'CSS'; }
64 pre.src-D:before { content: 'D'; }
65 pre.src-ditaa:before { content: 'ditaa'; }
66 pre.src-dot:before { content: 'Graphviz'; }
67 pre.src-calc:before { content: 'Emacs Calc'; }
68 pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
69 pre.src-fortran:before { content: 'Fortran'; }
70 pre.src-gnuplot:before { content: 'gnuplot'; }
71 pre.src-haskell:before { content: 'Haskell'; }
72 pre.src-hledger:before { content: 'hledger'; }
73 pre.src-java:before { content: 'Java'; }
74 pre.src-js:before { content: 'Javascript'; }
75 pre.src-latex:before { content: 'LaTeX'; }
76 pre.src-ledger:before { content: 'Ledger'; }
77 pre.src-lisp:before { content: 'Lisp'; }
78 pre.src-lilypond:before { content: 'Lilypond'; }
79 pre.src-lua:before { content: 'Lua'; }
80 pre.src-matlab:before { content: 'MATLAB'; }
81 pre.src-mscgen:before { content: 'Mscgen'; }
82 pre.src-ocaml:before { content: 'Objective Caml'; }
83 pre.src-octave:before { content: 'Octave'; }
84 pre.src-org:before { content: 'Org mode'; }
85 pre.src-oz:before { content: 'OZ'; }
86 pre.src-plantuml:before { content: 'Plantuml'; }
87 pre.src-processing:before { content: 'Processing.js'; }
88 pre.src-python:before { content: 'Python'; }
89 pre.src-R:before { content: 'R'; }
90 pre.src-ruby:before { content: 'Ruby'; }
91 pre.src-sass:before { content: 'Sass'; }
92 pre.src-scheme:before { content: 'Scheme'; }
93 pre.src-screen:before { content: 'Gnu Screen'; }
94 pre.src-sed:before { content: 'Sed'; }
95 pre.src-sh:before { content: 'shell'; }
96 pre.src-sql:before { content: 'SQL'; }
97 pre.src-sqlite:before { content: 'SQLite'; }
98 /* additional languages in org.el's org-babel-load-languages alist */
99 pre.src-forth:before { content: 'Forth'; }
100 pre.src-io:before { content: 'IO'; }
101 pre.src-J:before { content: 'J'; }
102 pre.src-makefile:before { content: 'Makefile'; }
103 pre.src-maxima:before { content: 'Maxima'; }
104 pre.src-perl:before { content: 'Perl'; }
105 pre.src-picolisp:before { content: 'Pico Lisp'; }
106 pre.src-scala:before { content: 'Scala'; }
107 pre.src-shell:before { content: 'Shell Script'; }
108 pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
109 /* additional language identifiers per "defun org-babel-execute"
111 pre.src-cpp:before { content: 'C++'; }
112 pre.src-abc:before { content: 'ABC'; }
113 pre.src-coq:before { content: 'Coq'; }
114 pre.src-groovy:before { content: 'Groovy'; }
115 /* additional language identifiers from org-babel-shell-names in
116 ob-shell.el: ob-shell is the only babel language using a lambda to put
117 the execution function name together. */
118 pre.src-bash:before { content: 'bash'; }
119 pre.src-csh:before { content: 'csh'; }
120 pre.src-ash:before { content: 'ash'; }
121 pre.src-dash:before { content: 'dash'; }
122 pre.src-ksh:before { content: 'ksh'; }
123 pre.src-mksh:before { content: 'mksh'; }
124 pre.src-posh:before { content: 'posh'; }
125 /* Additional Emacs modes also supported by the LaTeX listings package */
126 pre.src-ada:before { content: 'Ada'; }
127 pre.src-asm:before { content: 'Assembler'; }
128 pre.src-caml:before { content: 'Caml'; }
129 pre.src-delphi:before { content: 'Delphi'; }
130 pre.src-html:before { content: 'HTML'; }
131 pre.src-idl:before { content: 'IDL'; }
132 pre.src-mercury:before { content: 'Mercury'; }
133 pre.src-metapost:before { content: 'MetaPost'; }
134 pre.src-modula-2:before { content: 'Modula-2'; }
135 pre.src-pascal:before { content: 'Pascal'; }
136 pre.src-ps:before { content: 'PostScript'; }
137 pre.src-prolog:before { content: 'Prolog'; }
138 pre.src-simula:before { content: 'Simula'; }
139 pre.src-tcl:before { content: 'tcl'; }
140 pre.src-tex:before { content: 'TeX'; }
141 pre.src-plain-tex:before { content: 'Plain TeX'; }
142 pre.src-verilog:before { content: 'Verilog'; }
143 pre.src-vhdl:before { content: 'VHDL'; }
144 pre.src-xml:before { content: 'XML'; }
145 pre.src-nxml:before { content: 'XML'; }
146 /* add a generic configuration mode; LaTeX export needs an additional
147 (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
148 pre.src-conf:before { content: 'Configuration File'; }
150 table { border-collapse:collapse; }
151 caption.t-above { caption-side: top; }
152 caption.t-bottom { caption-side: bottom; }
153 td, th { vertical-align:top; }
154 th.org-right { text-align: center; }
155 th.org-left { text-align: center; }
156 th.org-center { text-align: center; }
157 td.org-right { text-align: right; }
158 td.org-left { text-align: left; }
159 td.org-center { text-align: center; }
160 dt { font-weight: bold; }
161 .footpara { display: inline; }
162 .footdef { margin-bottom: 1em; }
163 .figure { padding: 1em; }
164 .figure p { text-align: center; }
165 .equation-container {
171 vertical-align: middle;
176 vertical-align: middle;
180 border: 2px solid gray;
185 { text-align: right; font-size: 70%; white-space: nowrap; }
186 textarea { overflow-x: auto; }
187 .linenr { font-size: smaller }
188 .code-highlighted { background-color: #ffff00; }
189 .org-info-js_info-navigation { border-style: none; }
190 #org-info-js_console-label
191 { font-size: 10px; font-weight: bold; white-space: nowrap; }
192 .org-info-js_search-highlight
193 { background-color: #ffff00; color: #000000; font-weight: bold; }
196 <link rel="stylesheet" type="text/css" href="https://thomasf.github.io/solarized-css/solarized-dark.min.css" />
199 <div id="content" class="content">
200 <h1 class="title">Fifth - virtual machine</h1>
201 <div id="table-of-contents" role="doc-toc">
202 <h2>Table of Contents</h2>
203 <div id="text-table-of-contents" role="doc-toc">
205 <li><a href="#org5215c98">1. Instructions overview</a>
207 <li><a href="#orgad315b0">1.1. kbd@ - read scancode of pressed or released key</a></li>
208 <li><a href="#org944323a">1.2. vidput - put image1 into image2, at location x, y</a></li>
209 <li><a href="#org5b4d781">1.3. cmove - copy memory array</a></li>
210 <li><a href="#orgb1e3963">1.4. tvidput - put image with transparency support</a></li>
211 <li><a href="#orgac6e38a">1.5. charput - draw text character</a></li>
218 Current emulator emulates:
222 <li>It has 2 stacks</li>
223 <li>~50 instructions</li>
224 <li>4GB flat address space (theoretically).</li>
229 While I tried to keep instruction set simple, I was forced to put in
230 some complex instructions to make performance acceptable on
235 CPU has following registers:
238 <dt>IP</dt><dd>instruction pointer</dd>
239 <dt>DSP</dt><dd>data stack pointer</dd>
240 <dt>RSP</dt><dd>return stack pointer</dd>
243 <div id="outline-container-org5215c98" class="outline-2">
244 <h2 id="org5215c98"><span class="section-number-2">1.</span> Instructions overview</h2>
245 <div class="outline-text-2" id="text-1">
247 Virtual CPU, commands (most of them are avaiable as ordinary commands
248 in programming language):
252 <table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
256 <col class="org-right" />
258 <col class="org-left" />
260 <col class="org-left" />
262 <col class="org-left" />
266 <th scope="col" class="org-right">#</th>
267 <th scope="col" class="org-left">name</th>
268 <th scope="col" class="org-left">stack footprint</th>
269 <th scope="col" class="org-left">description</th>
274 <td class="org-right">0</td>
275 <td class="org-left">nop</td>
276 <td class="org-left">--</td>
277 <td class="org-left">does nothing</td>
281 <td class="org-right">1</td>
282 <td class="org-left">halt</td>
283 <td class="org-left">--</td>
284 <td class="org-left">halt CPU ( return to DOS on emulator )</td>
288 <td class="org-right">2</td>
289 <td class="org-left"><a href="#orgad315b0">kbd@</a></td>
290 <td class="org-left">– c</td>
291 <td class="org-left">read scancode of pressed or released key</td>
295 <td class="org-right">3</td>
296 <td class="org-left">num <dword></td>
297 <td class="org-left">– n</td>
298 <td class="org-left">put immidiate number into datastack</td>
302 <td class="org-right">4</td>
303 <td class="org-left">jmp <dword></td>
304 <td class="org-left">--</td>
305 <td class="org-left">jump to specified code</td>
309 <td class="org-right">5</td>
310 <td class="org-left">call <dword></td>
311 <td class="org-left"> </td>
312 <td class="org-left">jump to specified code, save return address to return stack</td>
316 <td class="org-right">6</td>
317 <td class="org-left">1+</td>
318 <td class="org-left">n – n+1</td>
319 <td class="org-left"> </td>
323 <td class="org-right">7</td>
324 <td class="org-left">1-</td>
325 <td class="org-left">n – n-1</td>
326 <td class="org-left"> </td>
330 <td class="org-right">8</td>
331 <td class="org-left">dup</td>
332 <td class="org-left">n – n n</td>
333 <td class="org-left">duplicate top of data stack</td>
337 <td class="org-right">9</td>
338 <td class="org-left">drop</td>
339 <td class="org-left">n --</td>
340 <td class="org-left">drop last element in data stack</td>
344 <td class="org-right">10</td>
345 <td class="org-left">if <dword></td>
346 <td class="org-left">n --</td>
347 <td class="org-left">jump to addr if top element was 0</td>
351 <td class="org-right">11</td>
352 <td class="org-left">ret</td>
353 <td class="org-left"> </td>
354 <td class="org-left">jump to code, specified in return stack.</td>
358 <td class="org-right">12</td>
359 <td class="org-left">c@</td>
360 <td class="org-left">addr – n</td>
361 <td class="org-left">read byte from memory at specified address</td>
365 <td class="org-right">13</td>
366 <td class="org-left">c!</td>
367 <td class="org-left">n addr --</td>
368 <td class="org-left">store byte to specified memory</td>
372 <td class="org-right">14</td>
373 <td class="org-left">push</td>
374 <td class="org-left">DSTK -> RSTK</td>
375 <td class="org-left">move top of datastack to returnstack</td>
379 <td class="org-right">15</td>
380 <td class="org-left">pop</td>
381 <td class="org-left">RSTK -> DSTK</td>
382 <td class="org-left">move top of returnstack to datastack</td>
386 <td class="org-right">16</td>
387 <td class="org-left"><unused></td>
388 <td class="org-left"> </td>
389 <td class="org-left"> </td>
393 <td class="org-right">17</td>
394 <td class="org-left">rot</td>
395 <td class="org-left">n1 n2 n3 – n2 n3 n1</td>
396 <td class="org-left">rotate stack elements</td>
400 <td class="org-right">18</td>
401 <td class="org-left">disk@</td>
402 <td class="org-left">FromDiskSect ToMem --</td>
403 <td class="org-left">read 1KB from disk into RAM</td>
407 <td class="org-right">19</td>
408 <td class="org-left">disk!</td>
409 <td class="org-left">FromMem ToDiskSect --</td>
410 <td class="org-left">write 1KB to disk</td>
414 <td class="org-right">20</td>
415 <td class="org-left">@</td>
416 <td class="org-left">addr – n</td>
417 <td class="org-left">read 32 bit number from memory</td>
421 <td class="org-right">21</td>
422 <td class="org-left">!</td>
423 <td class="org-left">n addr --</td>
424 <td class="org-left">store 32 bit number to memory</td>
428 <td class="org-right">22</td>
429 <td class="org-left">over</td>
430 <td class="org-left">n1 n2 – n1 n2 n1</td>
431 <td class="org-left"> </td>
435 <td class="org-right">23</td>
436 <td class="org-left">swap</td>
437 <td class="org-left">n1 n2 – n2 n1</td>
438 <td class="org-left"> </td>
442 <td class="org-right">24</td>
443 <td class="org-left">+</td>
444 <td class="org-left">n1 n2 – n1+n2</td>
445 <td class="org-left"> </td>
449 <td class="org-right">25</td>
450 <td class="org-left">-</td>
451 <td class="org-left">n1 n2 – n1-n2</td>
452 <td class="org-left"> </td>
456 <td class="org-right">26</td>
457 <td class="org-left">*</td>
458 <td class="org-left">n1 n2 – n1*n2</td>
459 <td class="org-left"> </td>
463 <td class="org-right">27</td>
464 <td class="org-left">/</td>
465 <td class="org-left">n1 n2 – n1/n2</td>
466 <td class="org-left"> </td>
470 <td class="org-right">28</td>
471 <td class="org-left">></td>
472 <td class="org-left">n1 n2 – result</td>
473 <td class="org-left">is true when n1 > n2</td>
477 <td class="org-right">29</td>
478 <td class="org-left"><</td>
479 <td class="org-left">n1 n2 – result</td>
480 <td class="org-left">is true when n1 < n2</td>
484 <td class="org-right">30</td>
485 <td class="org-left">not</td>
486 <td class="org-left">n1 – not_n1</td>
487 <td class="org-left">logical not</td>
491 <td class="org-right">31</td>
492 <td class="org-left">i</td>
493 <td class="org-left">– n</td>
494 <td class="org-left">copies top of return stack into datastack</td>
498 <td class="org-right">32</td>
499 <td class="org-left">cprt@</td>
500 <td class="org-left">addr – n</td>
501 <td class="org-left">read one byte from hardware port</td>
505 <td class="org-right">33</td>
506 <td class="org-left">cprt!</td>
507 <td class="org-left">n addr --</td>
508 <td class="org-left">store one byte to hardware port</td>
512 <td class="org-right">34</td>
513 <td class="org-left">i2</td>
514 <td class="org-left">– n</td>
515 <td class="org-left">like "i" but takes second top stack element</td>
519 <td class="org-right">35</td>
520 <td class="org-left">i3</td>
521 <td class="org-left">– n</td>
522 <td class="org-left">like "i" but takes third top stack element.</td>
526 <td class="org-right">36</td>
527 <td class="org-left">shl</td>
528 <td class="org-left">n amount – n</td>
529 <td class="org-left">left bit shift</td>
533 <td class="org-right">37</td>
534 <td class="org-left">shr</td>
535 <td class="org-left">n amount – n</td>
536 <td class="org-left">right bit shift</td>
540 <td class="org-right">38</td>
541 <td class="org-left">or</td>
542 <td class="org-left">n1 n2 – n</td>
543 <td class="org-left">logical or</td>
547 <td class="org-right">39</td>
548 <td class="org-left">xor</td>
549 <td class="org-left">n1 n2 – n</td>
550 <td class="org-left">exclusive logical or</td>
554 <td class="org-right">40</td>
555 <td class="org-left">vidmap</td>
556 <td class="org-left">addr --</td>
557 <td class="org-left">copy memory from "addr" to video memory.</td>
561 <td class="org-right">41</td>
562 <td class="org-left">mouse@</td>
563 <td class="org-left">– x y button</td>
564 <td class="org-left">read mouse coordinates & buttons</td>
568 <td class="org-right">42</td>
569 <td class="org-left"><a href="#org944323a">vidput</a></td>
570 <td class="org-left">addr1 addr2 x y --</td>
571 <td class="org-left">put image1 into image2, at location x, y</td>
575 <td class="org-right">43</td>
576 <td class="org-left"><a href="#org5b4d781">cmove</a></td>
577 <td class="org-left">addr1 addr2 amount</td>
578 <td class="org-left">move memory from addr1 to addr2</td>
582 <td class="org-right">44</td>
583 <td class="org-left">cfill</td>
584 <td class="org-left">c addr amount --</td>
585 <td class="org-left">fill memory starting at "addr" with "c" bytes.</td>
589 <td class="org-right">45</td>
590 <td class="org-left"><a href="#orgb1e3963">tvidput</a></td>
591 <td class="org-left">addr1 addr2 x y --</td>
592 <td class="org-left">put image with transparency support</td>
596 <td class="org-right">46</td>
597 <td class="org-left">depth</td>
598 <td class="org-left">– depth</td>
599 <td class="org-left">returns current depth of data stack.</td>
603 <td class="org-right">47</td>
604 <td class="org-left"><a href="#orgac6e38a">charput</a></td>
605 <td class="org-left">colorfg colorbg addrsrc addrdest x y</td>
606 <td class="org-left">draw text character</td>
612 <div id="outline-container-orgad315b0" class="outline-3">
613 <h3 id="orgad315b0"><span class="section-number-3">1.1.</span> kbd@ - read scancode of pressed or released key</h3>
614 <div class="outline-text-3" id="text-1-1">
616 Returns 0 if no data available.
620 <div id="outline-container-org944323a" class="outline-3">
621 <h3 id="org944323a"><span class="section-number-3">1.2.</span> vidput - put image1 into image2, at location x, y</h3>
622 <div class="outline-text-3" id="text-1-2">
624 Does clipping, so part of a big image can be mapped into smaller one.
628 <div id="outline-container-org5b4d781" class="outline-3">
629 <h3 id="org5b4d781"><span class="section-number-3">1.3.</span> cmove - copy memory array</h3>
630 <div class="outline-text-3" id="text-1-3">
632 Move memory from addr1 to addr2. If addr1 is greater than addr2 then
633 count address foward while moving, elseway starts from end and counts
634 backwards, so no data loss occurs when memory regions partially
639 <div id="outline-container-orgb1e3963" class="outline-3">
640 <h3 id="orgb1e3963"><span class="section-number-3">1.4.</span> tvidput - put image with transparency support</h3>
641 <div class="outline-text-3" id="text-1-4">
645 <pre class="example">
651 Put image1 into image2, at location x, y with transparency support
655 Color 255 in source image is treated as transparent.
659 <div id="outline-container-orgac6e38a" class="outline-3">
660 <h3 id="orgac6e38a"><span class="section-number-3">1.5.</span> charput - draw text character</h3>
661 <div class="outline-text-3" id="text-1-5">
663 Draw character to image buffer located at "addrdest" to specified x &
664 y location. Decodes 8 bytes from source to bits, used to draw
671 <div id="postamble" class="status">
672 <p class="author">Author: Svjatoslav Agejenko</p>
673 <p class="date">Created: 2023-09-18 Mon 01:46</p>
674 <p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>