Web based MIPS assembler and emulator

index.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. // --- START DEBUG ONLY CODE ---
  3. // const fs = require('fs')
  4. // const input = fs.readFileSync('test.asm', { encoding: 'utf-8' })
  5. // --- END DEBUG ONLY CODE ---
  6. const encoders = require('./encoders.js')
  7. const lines = raw => raw.split('\n').map(s => s.trim()).filter(s => !!s)
  8. const parse = lineArray => lineArray.map(line => {
  9. return {
  10. instruction: line.substr(0, line.indexOf(' ')),
  11. arguments: line.substr(line.indexOf(' ')+1).split(',').map(s => s.trim())
  12. }
  13. }).map(line => encoders[line.instruction].apply(null, line.arguments))
  14. // console.log(parse(lines(input)))
  15. module.exports = {
  16. 'lines': lines,
  17. 'parse': parse
  18. }
  19. },{"./encoders.js":4}],2:[function(require,module,exports){
  20. // put some stuff into memory
  21. const memory = require('../memory/memory.js')
  22. const utils = require('./utils.js')
  23. // Pads a string with 0's until it is the desired length
  24. const padAddress = (addr, len, ch) => (addr.length >= len) ?
  25. addr : ch.repeat(len-addr.length).concat(addr)
  26. const incrAddr = (addr, n) => padAddress((parseInt(addr, 2) + n).toString(2), 32, '0')
  27. const baseAddr = '0xbfc00000'
  28. const loadBootCode = (code) => {
  29. memory.clearMemory()
  30. code.map((c, i) => memory.storeWord(incrAddr(utils.hex2bin(baseAddr, 32), i*4), utils.hex2bin(c, 32)))
  31. }
  32. module.exports = {
  33. 'loadBootCode': loadBootCode
  34. }
  35. },{"../memory/memory.js":6,"./utils.js":5}],3:[function(require,module,exports){
  36. // This file should hold the DOM interaction code
  37. let hex = ''
  38. const assembler = require('./assembler.js')
  39. const bootloader = require('./bootload.js')
  40. const assembleButton = document.getElementById('assemble')
  41. const assembleButtonHandler = event => {
  42. const code = document.getElementById('code')
  43. const rawCode = code.value
  44. hex = assembler.parse(assembler.lines(rawCode))
  45. bootloader.loadBootCode(hex)
  46. console.log(hex.join('\n'))
  47. }
  48. assembleButton.addEventListener('click', assembleButtonHandler)
  49. },{"./assembler.js":1,"./bootload.js":2}],4:[function(require,module,exports){
  50. const utils = require('./utils.js')
  51. const regmap = [
  52. 'zero', 'at', 'v0', 'v1', 'a0', 'a1', 'a2', 'a3', 't0', 't1', 't2', 't3', 't4', 't5',
  53. 't6', 't7', 's0', 's1', 's2', 's3', 's4', 's5', 's6', 's7', 't8', 't9', 'k0',
  54. 'k1', 'gp', 'sp', 's8', 'ra' ]
  55. const getReg = (id) => {
  56. if (id.startsWith('$')) {
  57. id = id.slice(1)
  58. }
  59. if ((isNaN(id) === false && +id >= 0 && +id <= 31) || (id = regmap.indexOf(id)) > -1) {
  60. return id
  61. } else {
  62. utils.error("Invalid register: " + id)
  63. }
  64. }
  65. const encoders = {
  66. 'LUI': (rt, immediate) =>
  67. '0x'.concat(utils.bin2hex(
  68. '001111'.concat(
  69. '0'.repeat(5),
  70. utils.dec2binu(getReg(rt), 5),
  71. utils.hex2bin(immediate, 16)), 8)),
  72. 'ORI': (rt, rs, immediate) =>
  73. '0x'.concat(utils.bin2hex(
  74. '001101'.concat(
  75. utils.dec2binu(getReg(rs), 5),
  76. utils.dec2binu(getReg(rt), 5),
  77. utils.hex2bin(immediate, 16)), 8)),
  78. 'SW': (rt, offsetBase) => {
  79. const [offset, base] = utils.splitOffsetBase(offsetBase)
  80. return '0x'.concat(utils.bin2hex(
  81. '101011'.concat(
  82. utils.dec2binu(getReg(base), 5),
  83. utils.dec2binu(getReg(rt), 5),
  84. utils.int16_2bin(offset, 16)), 8))
  85. }
  86. }
  87. module.exports = encoders
  88. },{"./utils.js":5}],5:[function(require,module,exports){
  89. const error = (error) => console.error(error)
  90. const dec2binu = (dec, length) => {
  91. let unsigned = Math.abs(dec)
  92. let bin = unsigned.toString(2)
  93. if (bin.length >= length) {
  94. return bin
  95. } else {
  96. return '0'.repeat(length - bin.length).concat(bin)
  97. }
  98. }
  99. const int16_2bin = (int16) => {
  100. const signed32 = (int16 >>> 0).toString(2)
  101. if (signed32.length > 16) {
  102. return signed32.slice(16)
  103. } else {
  104. return '0'.repeat(16 - signed32.length).concat(signed32)
  105. }
  106. }
  107. const hex2bin = (hex, length) => {
  108. if (hex.startsWith('0x')) {
  109. return dec2binu(parseInt(hex.slice(2), 16), length)
  110. } else {
  111. console.error("Hex immediates must start with 0x")
  112. }
  113. }
  114. const bin2hex = (bin, length) => {
  115. let hex = parseInt(bin, 2).toString(16)
  116. if (hex.length >= length) {
  117. return hex
  118. } else {
  119. return '0'.repeat(length - hex.length).concat(hex)
  120. }
  121. }
  122. const splitOffsetBase = (offsetBase) => offsetBase.trim().slice(0, -1).split('(').map(s => s.trim())
  123. module.exports = {
  124. 'error': error,
  125. 'dec2binu': dec2binu,
  126. 'hex2bin': hex2bin,
  127. 'bin2hex': bin2hex,
  128. 'int16_2bin': int16_2bin,
  129. 'splitOffsetBase': splitOffsetBase
  130. }
  131. },{}],6:[function(require,module,exports){
  132. // At the moment the memory array is a simple array, this means that _most_ of the time we
  133. // will not be filling up the array and therefore will not be using much RAM, however if
  134. // the full 4gb of memory are used, then it might prove problematic, and I will likely
  135. // move to a server side memory model that persists most data to disk
  136. let mainMemory = {}
  137. // Pads a string with 0's until it is the desired length
  138. const padAddress = (addr, len, ch) => (addr.length >= len) ?
  139. addr : ch.repeat(len-addr.length).concat(addr)
  140. // Converts a string address to a number, adds 1, then back to a string to be padded to 32 bits
  141. const getNextAddr = addr => padAddress((parseInt(addr, 2) + 1).toString(2), 32, '0')
  142. // Load a byte as a 32 bit 0 extended word
  143. const loadByte = addr => padAddress(mainMemory[addr], 32, '0')
  144. // Load a halfword as a 32 bit 0 extended word
  145. const loadHalfWord = addr => padAddress(
  146. loadByte(getNextAddr(addr))
  147. .concat(loadByte(addr)), 32, '0')
  148. // load a full 32 bit word into memory
  149. const loadWord = addr => padAddress(
  150. loadHalfWord(getNextAddr(getNextAddr(addr))).concat(
  151. loadHalfWord(addr)), 32, '0')
  152. // Stores a single byte in the memory array
  153. const storeByte = (addr, data) => {
  154. if (data.length == 8) {
  155. mainMemory[addr] = data
  156. } else {
  157. console.error("data length should be 8bit for byte store")
  158. }
  159. }
  160. // Stores two bytes in memory array, with the lower bytes in the lower addresses
  161. const storeHalfWord = (addr, data) => {
  162. if (data.length == 16) {
  163. if (addr.slice(-1) === '0') {
  164. storeByte(addr, data.slice(8))
  165. storeByte(getNextAddr(addr), data.slice(0,8))
  166. } else {
  167. console.error("misaligned halfword assignments are not yet supported")
  168. }
  169. } else {
  170. console.error("data length should be 16bit for halfword store")
  171. }
  172. }
  173. // Stores two bytes in memory array, with the lower bytes in the lower addresses
  174. const storeWord = (addr, data) => {
  175. if (data.length != 32) return console.error("data length should be 32bit") // function is void anyway so returning undefined doesn't matter
  176. storeHalfWord(addr, data.slice(16))
  177. storeHalfWord(getNextAddr(getNextAddr(addr)), data.slice(0, 16))
  178. }
  179. module.exports = {
  180. storeWord: storeWord,
  181. storeByte: storeByte,
  182. storeHalfWord, storeHalfWord,
  183. loadByte: loadByte,
  184. loadHalfWord: loadHalfWord,
  185. loadWord: loadWord,
  186. clearMemory: () => mainMemory = {}
  187. }
  188. },{}]},{},[3]);