Colossus.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * Colossus - an emulation of the world's first electronic computer
  3. *
  4. * @author VirtualColossus [martin@virtualcolossus.co.uk]
  5. * @copyright Crown Copyright 2019
  6. * @license Apache-2.0
  7. */
  8. import {INIT_PATTERNS, ITA2_TABLE, ROTOR_SIZES} from "../lib/Lorenz.mjs";
  9. /**
  10. * Colossus simulator class.
  11. */
  12. export class ColossusComputer {
  13. /**
  14. * Construct a Colossus.
  15. *
  16. * @param {string} ciphertext
  17. * @param {string} pattern - named pattern of Chi, Mu and Psi wheels
  18. * @param {Object} qbusin - which data inputs are being sent to q bus - each can be null, plain or delta
  19. * @param {Object[]} qbusswitches - Q bus calculation switches, multiple rows
  20. * @param {Object} control - control switches which specify stepping modes
  21. * @param {Object} starts - rotor start positions
  22. */
  23. constructor(ciphertext, pattern, qbusin, qbusswitches, control, starts, settotal, limit) {
  24. this.ITAlookup = ITA2_TABLE;
  25. this.ReverseITAlookup = {};
  26. for (const letter in this.ITAlookup) {
  27. const code = this.ITAlookup[letter];
  28. this.ReverseITAlookup[code] = letter;
  29. }
  30. this.initThyratrons(pattern);
  31. this.ciphertext = ciphertext;
  32. this.qbusin = qbusin;
  33. this.qbusswitches = qbusswitches;
  34. this.control = control;
  35. this.starts = starts;
  36. this.settotal = settotal;
  37. this.limitations = limit;
  38. this.allCounters = [0, 0, 0, 0, 0];
  39. this.Zbits = [0, 0, 0, 0, 0]; // Z input is the cipher tape
  40. this.ZbitsOneBack = [0, 0, 0, 0, 0]; // for delta
  41. this.Qbits = [0, 0, 0, 0, 0]; // input generated for placing onto the Q-bus (the logic processor)
  42. this.Xbits = [0, 0, 0, 0, 0]; // X is the Chi wheel bits
  43. this.Xptr = [0, 0, 0, 0, 0]; // pointers to the current X bits (Chi wheels)
  44. this.XbitsOneBack = [0, 0, 0, 0, 0]; // the X bits one back (for delta)
  45. this.Sbits = [0, 0, 0, 0, 0]; // S is the Psi wheel bits
  46. this.Sptr = [0, 0, 0, 0, 0]; // pointers to the current S bits (Psi wheels)
  47. this.SbitsOneBack = [0, 0, 0, 0, 0]; // the S bits one back (for delta)
  48. this.Mptr = [0, 0];
  49. this.rotorPtrs = {};
  50. this.totalmotor = 0;
  51. this.P5Zbit = [0, 0];
  52. }
  53. /**
  54. * Begin a run
  55. *
  56. * @returns {object}
  57. */
  58. run() {
  59. const result = {
  60. printout: ""
  61. };
  62. // loop until our start positions are back to the beginning
  63. this.rotorPtrs = {X1: this.starts.X1, X2: this.starts.X2, X3: this.starts.X3, X4: this.starts.X4, X5: this.starts.X5, M61: this.starts.M61, M37: this.starts.M37, S1: this.starts.S1, S2: this.starts.S2, S3: this.starts.S3, S4: this.starts.S4, S5: this.starts.S5};
  64. // this.rotorPtrs = this.starts;
  65. let runcount = 1;
  66. const fast = this.control.fast;
  67. const slow = this.control.slow;
  68. // Print Headers
  69. result.printout += fast + " " + slow + "\n";
  70. do {
  71. this.allCounters = [0, 0, 0, 0, 0];
  72. this.ZbitsOneBack = [0, 0, 0, 0, 0];
  73. this.XbitsOneBack = [0, 0, 0, 0, 0];
  74. // Run full tape loop and process counters
  75. this.runTape();
  76. // Only print result if larger than set total
  77. let fastRef = "00";
  78. let slowRef = "00";
  79. if (fast !== "") fastRef = this.rotorPtrs[fast].toString().padStart(2, "0");
  80. if (slow !== "") slowRef = this.rotorPtrs[slow].toString().padStart(2, "0");
  81. let printline = "";
  82. for (let c=0;c<5;c++) {
  83. if (this.allCounters[c] > this.settotal) {
  84. printline += String.fromCharCode(c+97) + this.allCounters[c]+" ";
  85. }
  86. }
  87. if (printline !== "") {
  88. result.printout += fastRef + " " + slowRef + " : ";
  89. result.printout += printline;
  90. result.printout += "\n";
  91. }
  92. // Step fast rotor if required
  93. if (fast !== "") {
  94. this.rotorPtrs[fast]++;
  95. if (this.rotorPtrs[fast] > ROTOR_SIZES[fast]) this.rotorPtrs[fast] = 1;
  96. }
  97. // Step slow rotor if fast rotor has returned to initial start position
  98. if (slow !== "" && this.rotorPtrs[fast] === this.starts[fast]) {
  99. this.rotorPtrs[slow]++;
  100. if (this.rotorPtrs[slow] > ROTOR_SIZES[slow]) this.rotorPtrs[slow] = 1;
  101. }
  102. runcount++;
  103. } while (JSON.stringify(this.rotorPtrs) !== JSON.stringify(this.starts));
  104. result.counters = this.allCounters;
  105. result.runcount = runcount;
  106. return result;
  107. }
  108. /**
  109. * Run tape loop
  110. */
  111. runTape() {
  112. let charZin = "";
  113. this.Xptr = [this.rotorPtrs.X1, this.rotorPtrs.X2, this.rotorPtrs.X3, this.rotorPtrs.X4, this.rotorPtrs.X5];
  114. this.Mptr = [this.rotorPtrs.M37, this.rotorPtrs.M61];
  115. this.Sptr = [this.rotorPtrs.S1, this.rotorPtrs.S2, this.rotorPtrs.S3, this.rotorPtrs.S4, this.rotorPtrs.S5];
  116. // Run full loop of all character on the input tape (Z)
  117. for (let i=0; i<this.ciphertext.length; i++) {
  118. charZin = this.ciphertext.charAt(i);
  119. // Firstly, we check what inputs are specified on the Q-bus input switches
  120. this.getQbusInputs(charZin);
  121. /*
  122. * Pattern conditions on individual impulses. Matching patterns of bits on the Q bus.
  123. * This is the top section on Colussus K rack - the Q bus programming switches
  124. */
  125. const tmpcnt = this.runQbusProcessingConditional();
  126. /*
  127. * Addition of impulses.
  128. * This is the bottom section of Colossus K rack.
  129. */
  130. this.runQbusProcessingAddition(tmpcnt);
  131. // Store Z bit impulse 5 two back required for P5 limitation
  132. this.P5Zbit[1] = this.P5Zbit[0];
  133. this.P5Zbit[0] = this.ITAlookup[charZin].split("")[4];
  134. // Step rotors
  135. this.stepThyratrons();
  136. }
  137. }
  138. /**
  139. * Step thyratron rings to simulate movement of Lorenz rotors
  140. * Chi rotors all step one per character
  141. * Motor M61 rotor steps one per character, M37 steps dependant on M61 setting
  142. * Psi rotors only step dependant on M37 setting + limitation
  143. */
  144. stepThyratrons() {
  145. let X2bPtr = this.Xptr[1]-1;
  146. if (X2bPtr===0) X2bPtr = ROTOR_SIZES.X2;
  147. let S1bPtr = this.Sptr[0]-1;
  148. if (S1bPtr===0) S1bPtr = ROTOR_SIZES.S1;
  149. // Get Chi rotor 5 two back to calculate plaintext (Z+Chi+Psi=Plain)
  150. let X5bPtr=this.Xptr[4]-1;
  151. if (X5bPtr===0) X5bPtr=ROTOR_SIZES.X5;
  152. X5bPtr=X5bPtr-1;
  153. if (X5bPtr===0) X5bPtr=ROTOR_SIZES.X5;
  154. // Get Psi rotor 5 two back to calculate plaintext (Z+Chi+Psi=Plain)
  155. let S5bPtr=this.Sptr[4]-1;
  156. if (S5bPtr===0) S5bPtr=ROTOR_SIZES.S5;
  157. S5bPtr=S5bPtr-1;
  158. if (S5bPtr===0) S5bPtr=ROTOR_SIZES.S5;
  159. const x2sw = this.limitations.X2;
  160. const s1sw = this.limitations.S1;
  161. const p5sw = this.limitations.P5;
  162. // Limitation calculations
  163. let lim=1;
  164. if (x2sw) lim = this.rings.X[2][X2bPtr-1];
  165. if (s1sw) lim = lim ^ this.rings.S[1][S1bPtr-1];
  166. // P5
  167. if (p5sw) {
  168. let p5lim = this.P5Zbit[1];
  169. p5lim = p5lim ^ this.rings.X[5][X5bPtr-1];
  170. p5lim = p5lim ^ this.rings.S[5][S5bPtr-1];
  171. lim = lim ^ p5lim;
  172. }
  173. const basicmotor = this.rings.M[2][this.Mptr[0]-1];
  174. this.totalmotor = basicmotor;
  175. if (x2sw || s1sw) {
  176. if (basicmotor===0 && lim===1) {
  177. this.totalmotor = 0;
  178. } else {
  179. this.totalmotor = 1;
  180. }
  181. }
  182. // Step Chi rotors
  183. for (let r=0; r<5; r++) {
  184. this.Xptr[r]++;
  185. if (this.Xptr[r] > ROTOR_SIZES["X"+(r+1)]) this.Xptr[r] = 1;
  186. }
  187. if (this.totalmotor) {
  188. // Step Psi rotors
  189. for (let r=0; r<5; r++) {
  190. this.Sptr[r]++;
  191. if (this.Sptr[r] > ROTOR_SIZES["S"+(r+1)]) this.Sptr[r] = 1;
  192. }
  193. }
  194. // Move M37 rotor if M61 set
  195. if (this.rings.M[1][this.Mptr[1]-1]===1) this.Mptr[0]++;
  196. if (this.Mptr[0] > ROTOR_SIZES.M37) this.Mptr[0]=1;
  197. // Always move M61 rotor
  198. this.Mptr[1]++;
  199. if (this.Mptr[1] > ROTOR_SIZES.M61) this.Mptr[1]=1;
  200. }
  201. /**
  202. * Get Q bus inputs
  203. */
  204. getQbusInputs(charZin) {
  205. // Zbits - the bits from the current character from the cipher tape.
  206. this.Zbits = this.ITAlookup[charZin].split("");
  207. if (this.qbusin.Z === "Z") {
  208. // direct Z
  209. this.Qbits = this.Zbits;
  210. } else if (this.qbusin.Z === "ΔZ") {
  211. // delta Z, the Bitwise XOR of this character Zbits + last character Zbits
  212. for (let b=0;b<5;b++) {
  213. this.Qbits[b] = this.Zbits[b] ^ this.ZbitsOneBack[b];
  214. }
  215. }
  216. this.ZbitsOneBack = this.Zbits.slice(); // copy value of object, not reference
  217. // Xbits - the current Chi wheel bits
  218. for (let b=0;b<5;b++) {
  219. this.Xbits[b] = this.rings.X[b+1][this.Xptr[b]-1];
  220. }
  221. if (this.qbusin.Chi !== "") {
  222. if (this.qbusin.Chi === "Χ") {
  223. // direct X added to Qbits
  224. for (let b=0;b<5;b++) {
  225. this.Qbits[b] = this.Qbits[b] ^ this.Xbits[b];
  226. }
  227. } else if (this.qbusin.Chi === "ΔΧ") {
  228. // delta X
  229. for (let b=0;b<5;b++) {
  230. this.Qbits[b] = this.Qbits[b] ^ this.Xbits[b];
  231. this.Qbits[b] = this.Qbits[b] ^ this.XbitsOneBack[b];
  232. }
  233. }
  234. }
  235. this.XbitsOneBack = this.Xbits.slice();
  236. // Sbits - the current Psi wheel bits
  237. for (let b=0;b<5;b++) {
  238. this.Sbits[b] = this.rings.S[b+1][this.Sptr[b]-1];
  239. }
  240. if (this.qbusin.Psi !== "") {
  241. if (this.qbusin.Psi === "Ψ") {
  242. // direct S added to Qbits
  243. for (let b=0;b<5;b++) {
  244. this.Qbits[b] = this.Qbits[b] ^ this.Sbits[b];
  245. }
  246. } else if (this.qbusin.Psi === "ΔΨ") {
  247. // delta S
  248. for (let b=0;b<5;b++) {
  249. this.Qbits[b] = this.Qbits[b] ^ this.Sbits[b];
  250. this.Qbits[b] = this.Qbits[b] ^ this.SbitsOneBack[b];
  251. }
  252. }
  253. }
  254. this.SbitsOneBack = this.Sbits.slice();
  255. }
  256. /**
  257. * Conditional impulse Q bus section
  258. */
  259. runQbusProcessingConditional() {
  260. const cnt = [-1, -1, -1, -1, -1];
  261. const numrows = this.qbusswitches.condition.length;
  262. for (let r=0;r<numrows;r++) {
  263. const row = this.qbusswitches.condition[r];
  264. if (row.Counter !== "") {
  265. let result = true;
  266. const cPnt = row.Counter-1;
  267. const Qswitch = this.readBusSwitches(row.Qswitches);
  268. // Match switches to bit pattern
  269. for (let s=0;s<5;s++) {
  270. if (Qswitch[s] >= 0 && Qswitch[s] !== parseInt(this.Qbits[s], 10)) result = false;
  271. }
  272. // Check for NOT switch
  273. if (row.Negate) result = !result;
  274. // AND each row to get final result
  275. if (cnt[cPnt] === -1) {
  276. cnt[cPnt] = result;
  277. } else if (!result) {
  278. cnt[cPnt] = false;
  279. }
  280. }
  281. }
  282. // Negate the whole column, this allows A OR B by doing NOT(NOT A AND NOT B)
  283. for (let c=0;c<5;c++) {
  284. if (this.qbusswitches.condNegateAll && cnt[c] !== -1) cnt[c] = !cnt[c];
  285. }
  286. return cnt;
  287. }
  288. /**
  289. * Addition of impulses Q bus section
  290. */
  291. runQbusProcessingAddition(cnt) {
  292. const row = this.qbusswitches.addition[0];
  293. const Qswitch = row.Qswitches.slice();
  294. // To save making the arguments of this operation any larger, limiting addition counter to first one only
  295. // Colossus could actually add into any of the five counters.
  296. if (row.C1) {
  297. let addition = 0;
  298. for (let s=0;s<5;s++) {
  299. // XOR addition
  300. if (Qswitch[s]) {
  301. addition = addition ^ this.Qbits[s];
  302. }
  303. }
  304. const equals = (row.Equals===""?-1:(row.Equals==="."?0:1));
  305. if (addition === equals) {
  306. // AND with conditional rows to get final result
  307. if (cnt[0] === -1) cnt[0] = true;
  308. } else {
  309. cnt[0] = false;
  310. }
  311. }
  312. // Final check, check for addition section negate
  313. // then, if any column set, from top to bottom of rack, add to counter.
  314. for (let c=0;c<5;c++) {
  315. if (this.qbusswitches.addNegateAll && cnt[c] !== -1) cnt[c] = !cnt[c];
  316. if (this.qbusswitches.totalMotor === "" || (this.qbusswitches.totalMotor === "x" && this.totalmotor === 0) || (this.qbusswitches.totalMotor === "." && this.totalmotor === 1)) {
  317. if (cnt[c] === true) this.allCounters[c]++;
  318. }
  319. }
  320. }
  321. /**
  322. * Initialise thyratron rings
  323. * These hold the pattern of 1s & 0s for each rotor on banks of thyraton GT1C valves which act as a one-bit store.
  324. */
  325. initThyratrons(pattern) {
  326. this.rings = {
  327. X: {
  328. 1: INIT_PATTERNS[pattern].X[1].slice().reverse(),
  329. 2: INIT_PATTERNS[pattern].X[2].slice().reverse(),
  330. 3: INIT_PATTERNS[pattern].X[3].slice().reverse(),
  331. 4: INIT_PATTERNS[pattern].X[4].slice().reverse(),
  332. 5: INIT_PATTERNS[pattern].X[5].slice().reverse()
  333. },
  334. M: {
  335. 1: INIT_PATTERNS[pattern].M[1].slice().reverse(),
  336. 2: INIT_PATTERNS[pattern].M[2].slice().reverse(),
  337. },
  338. S: {
  339. 1: INIT_PATTERNS[pattern].S[1].slice().reverse(),
  340. 2: INIT_PATTERNS[pattern].S[2].slice().reverse(),
  341. 3: INIT_PATTERNS[pattern].S[3].slice().reverse(),
  342. 4: INIT_PATTERNS[pattern].S[4].slice().reverse(),
  343. 5: INIT_PATTERNS[pattern].S[5].slice().reverse()
  344. }
  345. };
  346. }
  347. /**
  348. * Read argument bus switches X & . and convert to 1 & 0
  349. */
  350. readBusSwitches(row) {
  351. const output = [-1, -1, -1, -1, -1];
  352. for (let c=0;c<5;c++) {
  353. if (row[c]===".") output[c] = 0;
  354. if (row[c]==="x") output[c] = 1;
  355. }
  356. return output;
  357. }
  358. }