sectorc.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <!doctype html>
  2. <title>v86: sectorc</title>
  3. <script src="../build/libv86.js"></script>
  4. <script>
  5. "use strict";
  6. window.onload = function()
  7. {
  8. const libc = `int tmp1;
  9. int tmp2;
  10. void shutdown()
  11. {
  12. /* Shutdown via APM: coded in asm machine code directly */
  13. // Check for APM
  14. // | mov ah,0x53; mov al,0x00; xor bx,bx; int 0x15; jc error
  15. asm 180; asm 83; asm 176; asm 0; asm 49; asm 219;
  16. asm 205; asm 21; asm 114; asm 55;
  17. // Disconnect from any APM interface
  18. // | mov ah,0x53; mov al,0x04; xor bx,bx; int 0x15
  19. // | jc maybe_error; jmp no_error
  20. asm 180; asm 83; asm 176; asm 4; asm 49; asm 219;
  21. asm 205; asm 21; asm 114; asm 2; asm 235; asm 5;
  22. // Label: maybe_error
  23. // | cmp ah,0x03; jne error
  24. asm 128; asm 252; asm 3; asm 117; asm 38;
  25. // Label: no_error
  26. // Connect to APM interface
  27. // | mov ah,0x53; mov al,0x01; xor bx,bx; int 0x15; jc error
  28. asm 180; asm 83; asm 176; asm 1; asm 49; asm 219;
  29. asm 205; asm 21; asm 114; asm 28;
  30. // Enable power management for all devices
  31. // | mov ah,0x53; mov al,0x08; mov bx,0x0001; mov cx,0x0001
  32. // | int 0x15; jc error
  33. asm 180; asm 83; asm 176; asm 8;
  34. asm 187; asm 1; asm 0; asm 185; asm 1; asm 0;
  35. asm 205; asm 21; asm 114; asm 14;
  36. // Set the power state for all devices
  37. // | mov ah,0x53; mov al,0x7; mov bx,0x0001; mov cx,0x0003
  38. // | int 0x15; jc error
  39. asm 180; asm 83; asm 176; asm 7;
  40. asm 187; asm 1; asm 0; asm 185; asm 3; asm 0;
  41. asm 205; asm 21; asm 114; asm 0;
  42. // Label: error
  43. // | hlt; jmp error
  44. asm 244; asm 235; asm 253;
  45. }
  46. int store_far_seg;
  47. int store_far_off;
  48. int store_far_val;
  49. void store_far()
  50. {
  51. // mov es, store_far_seg
  52. store_far_seg = store_far_seg;
  53. asm 142; asm 192;
  54. // mov si, store_far_off
  55. store_far_off = store_far_off;
  56. asm 137; asm 198;
  57. // mov es:[si], store_far_val
  58. store_far_val = store_far_val;
  59. asm 38; asm 137; asm 4;
  60. }
  61. int div10_unsigned_n;
  62. int div10_unsigned_q;
  63. int div10_unsigned_r;
  64. void div10_unsigned()
  65. {
  66. /* Taken from "Hacker's Delight", modified to "fit your screen" */
  67. tmp1 = ( div10_unsigned_n >> 1 ) & 32767; // unsigned
  68. tmp2 = ( div10_unsigned_n >> 2 ) & 16383; // unsigned
  69. div10_unsigned_q = tmp1 + tmp2;
  70. tmp1 = ( div10_unsigned_q >> 4 ) & 4095; // unsigned
  71. div10_unsigned_q = div10_unsigned_q + tmp1;
  72. tmp1 = ( div10_unsigned_q >> 8 ) & 255; // unsigned
  73. div10_unsigned_q = div10_unsigned_q + tmp1;
  74. div10_unsigned_q = ( div10_unsigned_q >> 3 ) & 8191; // unsigned
  75. div10_unsigned_r = div10_unsigned_n
  76. - ( ( div10_unsigned_q << 3 ) + ( div10_unsigned_q << 1 ) );
  77. if( div10_unsigned_r > 9 ){
  78. div10_unsigned_q = div10_unsigned_q + 1;
  79. div10_unsigned_r = div10_unsigned_r - 10;
  80. }
  81. }
  82. int print_ch;
  83. void print_char()
  84. {
  85. /* Implement print char via serial port bios function accessed via int 0x14 */
  86. print_ch = print_ch; // mov ax,[&print_ch]
  87. asm 180; asm 1; // mov ah,1
  88. asm 186; asm 0; asm 0 ; // mov dx,0
  89. asm 205; asm 20; // int 0x14
  90. }
  91. // uses 'print_ch'
  92. void print_newline()
  93. {
  94. print_ch = 10;
  95. print_char();
  96. }
  97. int print_num; // input
  98. int print_u16_bufptr;
  99. int print_u16_cur;
  100. void print_u16()
  101. {
  102. print_u16_bufptr = 30000; // buffer for ascii digits
  103. if( print_num == 0 ){
  104. print_ch = 48;
  105. print_char();
  106. }
  107. print_u16_cur = print_num;
  108. while( print_u16_cur != 0 ){
  109. div10_unsigned_n = print_u16_cur;
  110. div10_unsigned();
  111. *(int*) print_u16_bufptr = div10_unsigned_r;
  112. print_u16_bufptr = print_u16_bufptr + 1;
  113. print_u16_cur = div10_unsigned_q;
  114. }
  115. while( print_u16_bufptr != 30000 ){ // emit them in reverse over
  116. print_u16_bufptr = print_u16_bufptr - 1;
  117. print_ch = ( *(int*) print_u16_bufptr & 255 ) + 48;
  118. print_char();
  119. }
  120. }
  121. // uses 'print_num' and 'print_ch'
  122. void print_i16()
  123. {
  124. if( print_num < 0 ){
  125. print_ch = 45; print_char(); // '-'
  126. print_num = 0 - print_num;
  127. }
  128. print_u16();
  129. }
  130. void vga_init()
  131. {
  132. // mov ah,0; mov al,0x13; int 0x10
  133. asm 180; asm 0; asm 176; asm 19; asm 205; asm 16;
  134. }
  135. void vga_clear()
  136. {
  137. // push di; xor di,di; mov bx,0xa000; mov es,bx;
  138. // mov cx,0x7d00; xor ax,ax; rep stos; pop di
  139. asm 87 ; asm 49 ; asm 255; asm 187; asm 0; asm 160;
  140. asm 142; asm 195; asm 185; asm 0; asm 125; asm 49;
  141. asm 192; asm 243; asm 171; asm 95;
  142. }
  143. int pixel_x;
  144. int pixel_y;
  145. void vga_set_pixel()
  146. {
  147. // need to multiply pixel_y by 320 = 256 + 64
  148. // use 'tmp1' for pixel index
  149. tmp1 = ( ( pixel_y << 8 ) + ( pixel_y << 6 ) ) + pixel_x;
  150. // store to 0xa000:pixel_idx
  151. // mov bx,0xa000; mov es,bx; mov bx,ax; mov BYTE PTR es:[bx],0xf
  152. tmp1 = tmp1;
  153. asm 187; asm 0; asm 160; asm 142; asm 195;
  154. asm 137; asm 195; asm 38; asm 198; asm 7; asm 15;
  155. }
  156. int port_num;
  157. int port_val;
  158. void port_inb()
  159. {
  160. dx = port_num;
  161. // mov dx,WORD PTR [0x464]; in al,dx
  162. asm 139; asm 22; asm 160; asm 4; asm 236;
  163. // mov WORD PTR [0x464],ax
  164. asm 137; asm 6; asm 100; asm 4;
  165. port_val = ax;
  166. }
  167. void port_inw()
  168. {
  169. // mov dx,WORD PTR [0x464]; in ax,dx
  170. dx = port_num;
  171. asm 139; asm 22; asm 160; asm 4; asm 237;
  172. // mov WORD PTR [0x464],ax
  173. asm 137; asm 6; asm 100; asm 4;
  174. port_val = ax;
  175. }
  176. void port_outb()
  177. {
  178. dx = port_num;
  179. ax = port_val;
  180. // mov dx,WORD PTR [0x464]
  181. asm 139; asm 22; asm 160; asm 4;
  182. // mov ax,WORD PTR [0x464]
  183. asm 139; asm 6; asm 100; asm 4;
  184. // outb dx,al
  185. asm 238;
  186. }
  187. void port_outw()
  188. {
  189. dx = port_num;
  190. ax = port_val;
  191. // mov dx,WORD PTR [0x464]
  192. asm 139; asm 22; asm 160; asm 4;
  193. // mov ax,WORD PTR [0x464]
  194. asm 139; asm 6; asm 100; asm 4;
  195. // outb dx,al
  196. asm 239;
  197. }
  198. void dump_code_segment_and_shutdown()
  199. {
  200. /* NOTE: This code is in a different segment from data, and our compiled pointer accesses
  201. do not leave the data segment, so we need a little machine code to grab data from the
  202. code segment and stash it in a variable for C */
  203. i = 0;
  204. while( i < 8192 ){ /* Just assuming 8K is enough.. might not be true */
  205. // (put "i" in ax); mov si,ax; mov ax,cs:[si]; mov [&a],ax
  206. i = i; asm 137; asm 198; asm 46; asm 139; asm 4; asm 137; asm 133; asm 98; asm 0;
  207. print_ch = a;
  208. print_char();
  209. i = i + 1;
  210. }
  211. shutdown();
  212. }
  213. `;
  214. const start = `void _start()
  215. {
  216. main();
  217. shutdown();
  218. }
  219. `;
  220. const hello = `int buf;
  221. int ptr;
  222. int len;
  223. void vga_write()
  224. {
  225. /* Text vga is located at b800:0000 */
  226. store_far_seg = 47104; // segment: 0xb800
  227. store_far_off = idx << 1;
  228. store_far_val = ( 15 << 8 ) | ( ch & 255 ); // white fg and black bg
  229. store_far();
  230. }
  231. int x_off;
  232. int y_off;
  233. void vga_write_ch()
  234. {
  235. if( ch != 10 ){
  236. idx = y_off + x_off;
  237. vga_write();
  238. x_off = x_off + 1;
  239. }
  240. if( ( ch == 10 ) | ( x_off == 80 ) ){
  241. y_off = y_off + 80;
  242. x_off = 0;
  243. }
  244. }
  245. int idx;
  246. void vga_clear()
  247. {
  248. idx = 0;
  249. while( idx < 2000 ){ // 80x25
  250. ch = 32; // char: ' '
  251. vga_write();
  252. idx = idx + 1;
  253. }
  254. pos = 0;
  255. }
  256. void main()
  257. {
  258. // dump_code_segment_and_shutdown();
  259. vga_clear();
  260. ch = 72; vga_write_ch();
  261. ch = 101; vga_write_ch();
  262. ch = 108; vga_write_ch();
  263. ch = 108; vga_write_ch();
  264. ch = 111; vga_write_ch();
  265. ch = 10; vga_write_ch();
  266. ch = 32; vga_write_ch();
  267. ch = 102; vga_write_ch();
  268. ch = 114; vga_write_ch();
  269. ch = 111; vga_write_ch();
  270. ch = 109; vga_write_ch();
  271. ch = 10; vga_write_ch();
  272. ch = 32; vga_write_ch();
  273. ch = 32; vga_write_ch();
  274. ch = 83; vga_write_ch();
  275. ch = 101; vga_write_ch();
  276. ch = 99; vga_write_ch();
  277. ch = 116; vga_write_ch();
  278. ch = 111; vga_write_ch();
  279. ch = 114; vga_write_ch();
  280. ch = 67; vga_write_ch();
  281. ch = 10; vga_write_ch();
  282. ch = 32; vga_write_ch();
  283. ch = 32; vga_write_ch();
  284. ch = 32; vga_write_ch();
  285. i = 0;
  286. while( i < 10 ){
  287. ch = 33; vga_write_ch();
  288. i = i + 1;
  289. }
  290. while( 1 ){ }
  291. }
  292. `;
  293. const sinwave = `/* A Sine-wave Animation
  294. Math time:
  295. ---------------------------
  296. Along the range [0, pi] we can approximate sin(x) very crudely with a 2nd order quadratic
  297. That is: y = a * x^2 + b * x + c
  298. Three unknowns need three constraints, so picking the easy ones:
  299. x = 0, y = 0
  300. x = pi/2, y = 1
  301. x = pi, y = 0
  302. Solving the linear system:
  303. | 0 0 1 | | a | | 0 |
  304. | pi^2/4 pi/2 1 | * | b | = | 1 |
  305. | pi^2 pi 1 | | c | | 0 |
  306. We get:
  307. a = -4 / pi^2
  308. b = 4 / pi
  309. c = 0
  310. And:
  311. y = 4x(pi - x)/(pi^2)
  312. Engineering time:
  313. ---------------------------
  314. We are working with a 320x200 vga. We also don't have floating-point math. So, the
  315. goal here is to do all the math in integer screen coordinates and accept some pixel
  316. approximation error.
  317. First, we want to center the wave in the middle, y = 100
  318. We'll let y vary +-50 pixels to remain on the screen, so [50, 150]
  319. We want to show an entire cycle (2pi) on the x-axis, so *50 gives us [0, ~314]
  320. This implies that the "x-origin" is at x = 157
  321. Substituting in everything, we get:
  322. y ~= 100 + x*(157 - x)/125
  323. The division by 125 is problematic as we don't have division. But luckily 128 is close enough.
  324. Thus, we get:
  325. y ~= 100 + (x*(157 - x)) >> 7
  326. The rest is just adjusting for the [0, pi] range reduction by negating the approximation
  327. along [pi, 2pi]
  328. NOTE: the screen coordinate system is upside-down and I don't bother to correct for that.
  329. it simply means that the animation starts at a +pi phase offset
  330. */
  331. int y;
  332. int x;
  333. int x_0;
  334. void sin_positive_approx()
  335. {
  336. y = ( x_0 * ( 157 - x_0 ) ) >> 7;
  337. }
  338. void sin()
  339. {
  340. x_0 = x;
  341. while( x_0 > 314 ){
  342. x_0 = x_0 - 314;
  343. }
  344. if( x_0 <= 157 ){
  345. sin_positive_approx();
  346. }
  347. if( x_0 > 157 ){
  348. x_0 = x_0 - 157;
  349. sin_positive_approx();
  350. y = 0 - y;
  351. }
  352. y = 100 + y;
  353. }
  354. int offset;
  355. int x_end;
  356. void draw_sine_wave()
  357. {
  358. x = offset;
  359. x_end = x + 314;
  360. while( x <= x_end ){
  361. sin();
  362. pixel_x = x - offset;
  363. pixel_y = y;
  364. vga_set_pixel();
  365. x = x + 1;
  366. }
  367. }
  368. int v_1;
  369. int v_2;
  370. void delay()
  371. {
  372. v_1 = 0;
  373. while( v_1 < 50 ){
  374. v_2 = 0;
  375. while( v_2 < 10000 ){
  376. v_2 = v_2 + 1;
  377. }
  378. v_1 = v_1 + 1;
  379. }
  380. }
  381. void main()
  382. {
  383. vga_init();
  384. offset = 0;
  385. while( 1 ){
  386. vga_clear();
  387. draw_sine_wave();
  388. delay();
  389. offset = offset + 1;
  390. if( offset >= 314 ){ // mod the value to avoid 2^16 integer overflow
  391. offset = offset - 314;
  392. }
  393. }
  394. }
  395. `;
  396. const twinkle = `/* References:
  397. http://muruganad.com/8086/8086-assembly-language-program-to-play-sound-using-pc-speaker.html
  398. https://en.wikipedia.org/wiki/Twinkle,_Twinkle,_Little_Star
  399. */
  400. void delay_1()
  401. {
  402. v_1 = 0;
  403. while( v_1 < 4000 ){
  404. v_2 = 0;
  405. while( v_2 < 10000 ){
  406. v_2 = v_2 + 1;
  407. }
  408. v_1 = v_1 + 1;
  409. }
  410. }
  411. void delay_2()
  412. {
  413. v_1 = 0;
  414. while( v_1 < 300 ){
  415. v_2 = 0;
  416. while( v_2 < 10000 ){
  417. v_2 = v_2 + 1;
  418. }
  419. v_1 = v_1 + 1;
  420. }
  421. }
  422. void audio_init()
  423. {
  424. // Configure PIC2 mode
  425. port_num = 67;
  426. port_val = 182;
  427. port_outb();
  428. }
  429. void audio_enable()
  430. {
  431. // Set bits 0 and 1 to enable
  432. port_num = 97;
  433. port_inb();
  434. port_val = port_val | 3;
  435. port_outb();
  436. }
  437. void audio_disable()
  438. {
  439. // Clear bits 0 and 1 to enable
  440. port_num = 97;
  441. port_inb();
  442. port_val = port_val & 65532;
  443. port_outb();
  444. }
  445. int audio_freq;
  446. void audio_freq_set()
  447. {
  448. // Set frequency
  449. port_num = 66;
  450. port_val = audio_freq & 255;
  451. port_outb();
  452. port_val = ( audio_freq >> 8 ) & 255;
  453. port_outb();
  454. }
  455. int note;
  456. void play_quarter_note()
  457. {
  458. audio_freq = note;
  459. audio_freq_set();
  460. audio_enable();
  461. delay_1();
  462. audio_disable();
  463. delay_2();
  464. }
  465. void play_half_note()
  466. {
  467. audio_freq = note;
  468. audio_freq_set();
  469. audio_enable();
  470. delay_1();
  471. delay_1();
  472. audio_disable();
  473. delay_2();
  474. }
  475. void play_section_1()
  476. {
  477. note = C; play_quarter_note();
  478. note = C; play_quarter_note();
  479. note = G; play_quarter_note();
  480. note = G; play_quarter_note();
  481. note = A; play_quarter_note();
  482. note = A; play_quarter_note();
  483. note = G; play_half_note();
  484. note = F; play_quarter_note();
  485. note = F; play_quarter_note();
  486. note = E; play_quarter_note();
  487. note = E; play_quarter_note();
  488. note = D; play_quarter_note();
  489. note = D; play_quarter_note();
  490. note = C; play_half_note();
  491. }
  492. void play_section_2()
  493. {
  494. note = G; play_quarter_note();
  495. note = G; play_quarter_note();
  496. note = F; play_quarter_note();
  497. note = F; play_quarter_note();
  498. note = E; play_quarter_note();
  499. note = E; play_quarter_note();
  500. note = D; play_half_note();
  501. note = G; play_quarter_note();
  502. note = G; play_quarter_note();
  503. note = F; play_quarter_note();
  504. note = F; play_quarter_note();
  505. note = E; play_quarter_note();
  506. note = E; play_quarter_note();
  507. note = D; play_half_note();
  508. }
  509. void main()
  510. {
  511. audio_init();
  512. audio_enable();
  513. C = 4560;
  514. D = 4063;
  515. E = 3619;
  516. F = 3416;
  517. G = 3043;
  518. A = 2711;
  519. play_section_1();
  520. play_section_2();
  521. play_section_1();
  522. audio_disable();
  523. }
  524. `;
  525. document.getElementById("source").onkeydown = function(e)
  526. {
  527. if(e.which == 13 && e.ctrlKey)
  528. {
  529. document.getElementById("run").onclick();
  530. }
  531. };
  532. document.getElementById("source").textContent = sinwave;
  533. document.getElementById("source").onkeydown = function(e)
  534. {
  535. if(e.which == 13 && e.ctrlKey)
  536. {
  537. document.getElementById("run").onclick();
  538. }
  539. };
  540. document.getElementById("examples").onchange = function()
  541. {
  542. document.getElementById("source").textContent = { sinwave, twinkle, hello }[this.value];
  543. };
  544. let emulator;
  545. document.getElementById("run").onclick = run;
  546. function run()
  547. {
  548. emulator && emulator.destroy();
  549. emulator = window.emulator = new V86({
  550. wasm_path: "../build/v86.wasm",
  551. memory_size: 32 * 1024 * 1024,
  552. vga_memory_size: 2 * 1024 * 1024,
  553. screen_container: document.getElementById("screen_container"),
  554. bios: { url: "../bios/seabios.bin" },
  555. vga_bios: { url: "../bios/vgabios.bin" },
  556. fda: { url: "../images/sectorc.bin" },
  557. autostart: true,
  558. });
  559. emulator.add_listener("emulator-ready", () => {
  560. const source = libc + document.getElementById("source").value + start;
  561. emulator.serial0_send(source);
  562. });
  563. document.getElementById("run").onclick = stop;
  564. document.getElementById("run").textContent = "stop";
  565. };
  566. function stop()
  567. {
  568. emulator && emulator.destroy();
  569. document.getElementById("run").onclick = run;
  570. document.getElementById("run").textContent = "run (ctrl-enter)";
  571. }
  572. }
  573. </script>
  574. <br>
  575. <textarea id=source rows=20 cols=80>
  576. </textarea>
  577. <br>
  578. <select id=examples>
  579. <option value=sinwave>Sine wave</option>
  580. <option value=hello>Hello</option>
  581. <option value=twinkle>Twinkle (audio)</option>
  582. </select>
  583. <button id=run>run (ctrl-enter)</button>
  584. <br>
  585. <hr>
  586. <div id="screen_container">
  587. <div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
  588. <canvas style="display: none"></canvas>
  589. </div>
  590. <hr>
  591. <a href="https://github.com/xorvoid/sectorc">sectorc</a> on <a href="/v86/">v86</a>