ColdWater0

C-Base

Sep 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dw 0xABCD
  2. POST:
  3.     mov r0, 0       ;testing hardware
  4.     add r0, 8
  5.     sub r0, 7
  6.     shl r0, 4
  7.     shr r0, 2
  8.     cmp r0, 4
  9.     cmp r0, 4
  10.     cmp r0, 4
  11.     cmp r0, 4
  12.     jne .post_failure_halt
  13.     jne .post_failure_halt
  14.     jne .post_failure_halt
  15.     jne .post_failure_halt
  16.     jmp .post_passed
  17.  
  18.     .post_failure_halt:
  19.  
  20.     hlt
  21.     jmp .post_failure_halt
  22.     jmp .post_failure_halt
  23.     jmp .post_failure_halt
  24.     jmp .post_failure_halt
  25.    
  26. .post_passed:
  27.  
  28. bootstrap:
  29.     mov sp, 0x0700
  30.     call clear_reg
  31.     mov r10, [terminal_port]
  32.     call clear_screen
  33.     call splash
  34.     send r10, 0x20
  35.     call detect_model
  36.     send r10, 'K'
  37.     mov r0, [last_state]
  38.     jnz system_check
  39.     mov [last_state], 1
  40.     jmp main
  41.  
  42. splash:
  43.     send r10, 0x200F
  44.     send r10, 0x1000
  45.     send r10, 'C'
  46.     send r10, '-'
  47.     send r10, 'b'
  48.     send r10, 'a'
  49.     send r10, 's'
  50.     send r10, 'e'
  51.     ret
  52.  
  53. detect_model:
  54.     mov r0, [2048]
  55.     cmp r0, 0xABCD
  56.     je .k2
  57.     mov r0, [4096]
  58.     cmp r0, 0xABCD
  59.     je .k4
  60.     mov r0, [8192]
  61.     cmp r0, 0xABCD
  62.     je .k8
  63.     ret
  64.     .k2:
  65.         send r10, '2'
  66.         ret
  67.     .k4:
  68.         send r10, '4'
  69.         ret
  70.     .k8:
  71.         send r10, '8'
  72.         ret
  73.        
  74. shutdown:
  75.     mov [last_state], 0
  76.     mov r0, .str
  77.     call output_str
  78.     call clear_screen
  79.     hlt
  80.     mov ip, 0
  81.     mov ip, 0
  82.     .str:
  83.         dw 0x10FD, "...", 0
  84. reboot:
  85.     mov [last_state], 0
  86.     jmp POST
  87. system_check:
  88.     mov r0, .chk_msg
  89.     call output_str
  90.     cmp [operate_mode], 2
  91.     je reboot
  92.     call shutdown
  93.     .chk_msg:
  94.         dw 0x1000, "system check", 0
  95.  
  96.        
  97. ;C.base configurations
  98. terminal_color:
  99.     dw 0x200F   ;default = 0x200F
  100. terminal_port:
  101.     dw 0x0000   ;default = 0x200F
  102. last_state:
  103.     dw 0        ;0:successfully shutdowned
  104.                 ;1:while running, suddenly shutdowned
  105.                 ;2:shutdowned in critical moment
  106. operate_mode:
  107.     dw 2 ;0: normaly operate
  108.             ;2: ignore last_state flag
  109. ;C.base functions
  110.  
  111. clear_reg:  ;zero-fil registers
  112.     mov r0, 0
  113.     mov r1, 0
  114.     mov r2, 0
  115.     mov r3, 0
  116.     mov r4, 0
  117.     mov r5, 0
  118.     mov r6, 0
  119.     mov r7, 0
  120.     mov r8, 0
  121.     mov r9, 0
  122.     mov r10, 0
  123.     mov r11, 0
  124.     mov r12, 0
  125.     mov r13, 0
  126.     ret
  127.  
  128. input_char:
  129.     .wait_loop:
  130.         wait r0
  131.         js .wait_loop
  132.         bump r10
  133.     .bump_loop:
  134.         recv r0, r10
  135.         jnc .bump_loop
  136.     ret
  137.    
  138. input_char_blink:
  139.     mov [.cursor_p], r0
  140.     push r1
  141.     mov r1, 0x5F
  142.     .wait_loop:
  143.         sub [.t], 1
  144.         js .timer
  145.         wait r0
  146.         js .wait_loop
  147.         bump r10
  148.     .bump_loop:
  149.         recv r0, r10
  150.         jnc .bump_loop
  151.     ret
  152.     .t:
  153.         dw 4
  154.     .cursor_p:
  155.         dw 0
  156.     .timer:
  157.         send r10, [.cursor_p]
  158.         send r10, r1
  159.         xor r1, 0x7F
  160.         mov [.t], 4
  161.         jmp .wait_loop
  162.  
  163. input_str:
  164.     mov [.wall], r0
  165.     send r10, 0x7F
  166.     add r1, r0  ;r0: string space address
  167.     .wait_loop: ;r1: max length
  168.         wait r3 ;r2: terminal cursor
  169.         js .wait_loop
  170.         bump r10
  171.     .bump_loop:
  172.         recv r3, r10
  173.         jnc .bump_loop
  174.     cmp r3, 0x0D
  175.     je .enter
  176.     cmp r3, 0x08
  177.     je .bs
  178.     .ok:
  179.         cmp r1, r0
  180.         je .wait_loop
  181.         mov [r0], r3
  182.         send r10, r2
  183.         send r10, r3
  184.         send r10, 170
  185.         add r0, 1
  186.         add r2, 1
  187.         jmp .wait_loop
  188.     .bs:
  189.         cmp [.wall], r0
  190.         je .wait_loop
  191.         sub r0, 1
  192.         sub r2, 1
  193.         mov [r0], 0
  194.         send r10, r2
  195.         send r10, 170
  196.         send r10, 0x20
  197.         jmp .wait_loop
  198.     .enter:
  199.         ret
  200.     .wall: dw 0
  201.  
  202. input_str_blink:
  203.     mov [.wall], r0
  204.     mov r4, 0x5F
  205.     mov [.t], 4
  206.     add r1, r0  ;r0: string space address
  207.     .wait_loop: ;r1: max length
  208.         sub [.t], 1
  209.         js .time
  210.         wait r3 ;r2: terminal cursor
  211.         js .wait_loop
  212.         bump r10
  213.     .bump_loop:     ;r4: cursor character
  214.         recv r3, r10
  215.         jnc .bump_loop
  216.     cmp r3, 0x0D
  217.     je .enter
  218.     cmp r3, 0x08
  219.     je .bs
  220.     .ok:
  221.         cmp r1, r0
  222.         je .limit_length
  223.         mov [r0], r3
  224.         send r10, r2
  225.         send r10, r3
  226.         add r0, 1
  227.         add r2, 1
  228.         jmp .wait_loop
  229.     .bs:
  230.         cmp [.wall], r0
  231.         je .wait_loop
  232.         send r10, [terminal_color]
  233.         sub r0, 1
  234.         sub r2, 1
  235.         mov [r0], 0
  236.         send r10, r2
  237.         send r10, 0x20
  238.         send r10, 0x20
  239.         jmp .wait_loop
  240.     .enter:
  241.         send r10, r2
  242.         send r10, 0x20
  243.         send r10, [terminal_color]
  244.         ret
  245.     .wall: dw 0
  246.     .t: dw 4   
  247.     .time:
  248.         send r10, r2
  249.         send r10, r4
  250.         mov [.t], 4
  251.         xor r4, 0x7F
  252.         jmp .wait_loop
  253.     .limit_length:
  254.         send r10, 0x200C
  255.         jmp .wait_loop
  256.        
  257. input_str_blink_opt:
  258.     mov [.wall], r0
  259.     mov r4, 0x7F
  260.     mov [.t], 4
  261.     add r1, r0  ;r0: string space address
  262.     jmp .wait_loop
  263.     .limit_length:
  264.         send r10, 0x200C
  265.     .wait_loop: ;r1: max length
  266.         sub [.t], 1
  267.         jne .keepgoing
  268.         .time:
  269.         send r10, r2
  270.         send r10, r4
  271.         mov [.t], 4
  272.         xor r4, 0x5F
  273.     .keepgoing:
  274.         wait r3 ;r2: terminal cursor
  275.         js .wait_loop
  276.         bump r10
  277.     .bump_loop:     ;r4: cursor character
  278.         recv r3, r10
  279.         jnc .bump_loop
  280.     cmp r3, 0x0D
  281.     je .enter
  282.     cmp r3, 0x08
  283.     je .bs
  284.     .ok:
  285.         cmp r1, r0
  286.         je .limit_length
  287.         mov [r0], r3
  288.         send r10, r2
  289.         send r10, r3
  290.         add r0, 1
  291.         add r2, 1
  292.         jmp .wait_loop
  293.     .bs:
  294.         cmp [.wall], r0
  295.         je .wait_loop
  296.         send r10, [terminal_color]
  297.         sub r0, 1
  298.         sub r2, 1
  299.         mov [r0], 0
  300.         send r10, r2
  301.         send r10, 0x20
  302.         send r10, 0x20
  303.         jmp .wait_loop
  304.     .enter:
  305.         send r10, [terminal_color]
  306.         ret
  307.     .wall: dw 0
  308.     .t: dw 4
  309.        
  310.  
  311. output_str:
  312.     push r1                             ;r0: string address
  313.     .loop:
  314.         mov r1, [r0]
  315.         jz .exit
  316.         send r10, r1
  317.         add r0, 1
  318.         jmp .loop
  319.     .exit:
  320.         pop r1
  321.         ret
  322.        
  323. output_str_opt:
  324.     push r1                             ;r0: string address
  325.     sub r0,1
  326.     .loop:
  327.         add r0, 1
  328.         mov r1, [r0]
  329.         send r10, r1
  330.         jnz .loop
  331.     .exit:
  332.         pop r1
  333.         ret
  334.  
  335. o_hex:
  336.     push r1                             ;r0: number to display
  337.     mov r1, .str
  338.     call bin2hex
  339.     push r0
  340.     mov r0, .str
  341.     call output_str
  342.     pop r0
  343.     pop r1
  344.     ret
  345.     .str:
  346.         dw 0, 0, 0, 0, 0
  347.  
  348. bin2hex:
  349.     push r2
  350.     push r3
  351.     mov r3, .hexdata                    ;r0: target number
  352.     mov r2, r0                          ;r1: string space (5 cells)
  353.     and r2, 0xF000
  354.     shr r2, 12
  355.     mov r2, [r2+r3]
  356.     mov [r1], r2
  357.     mov r2, r0
  358.     and r2, 0x0F00
  359.     shr r2, 8
  360.     mov r2, [r2+r3]
  361.     mov [r1+1], r2
  362.     mov r2, r0
  363.     and r2, 0x00F0
  364.     shr r2, 4
  365.     mov r2, [r2+r3]
  366.     mov [r1+2], r2
  367.     mov r2, r0
  368.     and r2, 0x000F
  369.     mov r2, [r2+r3]
  370.     mov [r1+3], r2
  371.     mov [r1+4], 0
  372.     pop r3
  373.     pop r2
  374.     ret
  375.     .hexdata:
  376.         dw "0123456789ABCDEF"
  377.        
  378. clear_screen:
  379.     push r0
  380.     mov r0, 0x1000
  381.     shl [.point], 4
  382.     add [.point], r0
  383.     pop r0
  384.     send r10, [.point]
  385.     send r10, [terminal_color]
  386.     .loop:     
  387.         send r10, 0x20
  388.         send r10, 0x20
  389.         send r10, 0x20
  390.         send r10, 0x20
  391.         send r10, 0x20
  392.         send r10, 0x20
  393.         send r10, 0x20
  394.         send r10, 0x20
  395.         send r10, 0x20
  396.         send r10, 0x20
  397.         send r10, 0x20
  398.         send r10, 0x20
  399.         send r10, 0x20
  400.         send r10, 0x20
  401.         send r10, 0x20
  402.         send r10, 0x20
  403.         sub [.lines], 1
  404.         jnz .loop
  405.     .exit:
  406.         mov [.point], 0
  407.         mov [.lines], 12
  408.         ret
  409.     .point:
  410.         dw 0
  411.     .lines:
  412.         dw 12
  413.        
  414. clear_line:
  415.     send r10, 0x20
  416.     send r10, 0x20
  417.     send r10, 0x20
  418.     send r10, 0x20
  419.     send r10, 0x20
  420.     send r10, 0x20
  421.     send r10, 0x20
  422.     send r10, 0x20
  423.     send r10, 0x20
  424.     send r10, 0x20
  425.     send r10, 0x20
  426.     send r10, 0x20
  427.     send r10, 0x20
  428.     send r10, 0x20
  429.     send r10, 0x20
  430.     send r10, 0x20
  431.     ret
  432.  
  433. ;C.base external functions
  434.  
  435. ;C.base variables
  436.  
  437. Cbase_end:
  438.    
  439. main:
  440. C_UI:                       ;C-UI : WIP
  441.     call .show_page
  442.     call .input
  443.     call .move_cursor
  444.    
  445.    
  446. .crnt_cursor:
  447.     dw 0
  448. .prev_cursor:
  449.     dw 0
  450.    
  451. .show_page:
  452.     mov r2, program_titles
  453.     mov r0, [r2]
  454.     .loop:
  455.         jz .next
  456.         call output_str_opt
  457.         add r2, 1
  458.         mov r0, [r2]
  459.         jmp .loop
  460.     .next:
  461.         ret
  462.        
  463. .move_cursor:
  464.     mov r0, [.prev_cursor]
  465.     add r0, program_titles
  466.     mov r0, [r0]
  467.     mov r0, [r0]
  468.     sub r0, 1
  469.     send r10, r0
  470.     send r10, 0x20
  471.     mov r0, [.crnt_cursor]
  472.     add r0, program_titles
  473.     mov r0, [r0]
  474.     mov r0, [r0]
  475.     sub r0, 1
  476.     send r10, r0
  477.     send r10, 194
  478.     ret
  479.    
  480.    
  481.    
  482. .input:
  483.     call input_char
  484.     cmp r0, 'w'
  485.     je .up
  486.     cmp r0, 's'
  487.     je .down
  488.     cmp r0, 13
  489.     je .enter
  490.     jmp .input
  491.  
  492. .up:
  493.     mov r0, [.crnt_cursor]
  494.     cmp r0, 0
  495.     je .input
  496.    
  497.     mov [.prev_cursor], r0
  498.     sub [.crnt_cursor], 1
  499.     ret
  500. .down:
  501.     mov r0, [.crnt_cursor]
  502.     cmp r0, 1
  503.     je .input
  504.    
  505.     mov [.prev_cursor], r0
  506.     add [.crnt_cursor], 1
  507.     ret
  508.    
  509. .enter:
  510.     mov r0, [.crnt_cursor]
  511.     add r0, program_addr
  512.     jmp [r0]
  513.  
  514. program_addr:
  515.     dw Test, Config, 0
  516. program_titles:
  517.     dw .title0, .title1, 0
  518.     .title0:
  519.         dw 0x1043, " Test", 0
  520.     .title1:
  521.         dw 0x1063, " Config", 0
  522.        
  523. Test:
  524.     mov r0, .str
  525.     call output_str
  526.     call shutdown
  527.     .str: dw 0x1000, "Test", 0
  528. Config:
  529.     mov r0, .str
  530.     call output_str
  531.     call shutdown
  532.     .str: dw 0x1000, "Config", 0
Add Comment
Please, Sign In to add comment