Web based MIPS assembler and emulator

index.js 7.1KB

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