Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REM ================================
- REM * NETLIB v2.0
- REM The base library for NET code
- REM ================================
- NETLIB:
- REM --------
- REM # Init
- REM --------
- REM # Turn on monitor
- SET A, 0
- SET B, [@ScreenStart]
- HWI 0
- REM # Setup screen
- JSR ClearScreen
- JSR ResetCursorChar
- REM # Setup interrupts
- IAS HardwareIterrupt
- REM # Start code
- SET PC, [@CodeStart]
- REM ##############
- REM NETLIB Vars
- REM ##############
- @CodeStart: DAT Start
- @ScreenStart: DAT 0x8000
- @NextLine: DAT 0x7FE0
- @NextPrint: DAT 0x7FE0
- @ScreenEnd: DAT 0x817F
- @PacketHandler: DAT 0x0000
- @KeyHandler: DAT 0x0000
- @TimeoutAddress: DAT 0x0000
- @TimeoutTicks: DAT 0x0000
- @HandlerReg: DAT 0x0000
- @IP: DAT 0x0000
- @ClearStack: DAT 0x0000
- @CursorChar: DAT 0x0000
- @Input:
- DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- $InvalidInput:
- DAT "Invalid input", 0x0
- $Failed:
- DAT "Failed", 0x0
- REM #- ResetCursorChar -###########
- REM Reset to default @CursorChar
- REM ###############################
- ResetCursorChar:
- SET [@CursorChar], 0x193E
- SET PC, POP
- REM #- WaitForIP -#################
- REM Wait until you get an IP
- REM ###############################
- WaitForIP:
- SET A, 9
- SET B, 0xffff
- WaitForIP_hang:
- HWI 4
- IFE B, 0xffff
- MOV WaitForIP_hang
- REM # Store IP
- SET [@IP], B
- SET PC, POP
- REM #- LoadNextLine -##############
- REM Loads the address of the next
- REM line into B
- REM ###############################
- LoadNextLine:
- ADD [@NextLine], 0x20
- IFG [@NextLine], [@ScreenEnd]
- JSR ScrollUp
- REM # Set return register
- SET B, [@NextLine]
- REM # Update next print
- SET [@NextPrint], B
- SET PC, POP
- REM #- ScrollUp -####################
- REM Scroll the screen up a line
- REM #################################
- ScrollUp:
- SET PUSH, C
- SET PUSH, I
- SET I, [@ScreenStart]
- SET C, [@ScreenEnd]
- SUB C, 0x1F
- REM # Store updated position
- SET [@NextLine], C
- SET [@NextPrint], C
- ScrollUp_loop:
- SET [I], [I + 0x20]
- SET [I + 0x20], 0x1920
- ADD I, 1
- IFL I, C
- SET PC, ScrollUp_loop
- SET I, POP
- SET C, POP
- SET PC, POP
- REM #- Print -#####################
- REM Prints null-string at [A]
- REM continuing at the last point
- REM ###############################
- Print:
- SET PUSH, A
- SET PUSH, B
- SET B, [@NextPrint]
- JSR PrintStr_A
- SET B, POP
- SET A, POP
- SET PC, POP
- REM #- PrintLn -###################
- REM Print null-str at [A] on a
- REM new line
- REM ###############################
- PrintLn:
- SET PUSH, A
- SET PUSH, B
- REM # Load line to B
- JSR LoadNextLine
- JSR PrintStr_A
- SET B, POP
- SET A, POP
- SET PC, POP
- REM #- PrintStr -##################
- REM Print null-str at [A] starting
- REM at [B]. Target point 'B' will
- REM be shifted on return.
- REM ###############################
- PrintStr:
- SET PUSH, A
- JSR PrintStr_A
- SET A, POP
- SET PC, POP
- REM #- PrintStr_A -################
- REM Same as PrintStr, but input
- REM point 'A' will also be shifted
- REM ###############################
- PrintStr_A:
- SET PUSH, C
- PrintStr_A_loop:
- IFE [A], 0
- MOV PrintStr_A_exit
- SET C, [A]
- BOR C, 0x1900
- SET [B], C
- ADD A, 1
- ADD B, 1
- MOV PrintStr_A_loop
- PrintStr_A_exit:
- REM # Past null termination
- ADD A, 1
- REM # Update next print position
- SET [@NextPrint], B
- SET C, POP
- SET PC, POP
- REM #-PrintHex -###################
- REM Print a hex number A to [B]
- REM ###############################
- PrintHex:
- PUSHA
- REM # "0x"
- SET [B], 0x1930
- ADD B, 1
- SET [B], 0x1978
- ADD B, 1
- SET I, 12
- PrintHex_loop:
- SET C, A
- SHR C, I
- AND C, 0xf
- IFG C, 0x9
- ADD C, 0x37
- IFL C, 0xa
- ADD C, 0x30
- BOR C, 0x1900
- SET [B], C
- ADD B, 1
- SUB I, 4
- IFA I, 0xffff
- SET PC, PrintHex_loop
- POPA
- SET PC, POP
- REM #- PrintChar -###################
- REM Print a character C to [B++]
- REM NOTE: Doesn't effect @NextPrint
- REM #################################
- PrintChar:
- SET PUSH, A
- REM # Check for end of line
- SET A, [@NextLine]
- ADD A, 0x1E
- IFG B, A
- MOV PrintChar_skip
- SET [B], C
- BOR [B], 0x1900
- ADD B, 1
- PrintChar_skip:
- SET [B], 0x199D
- SET A, POP
- SET PC, POP
- REM #- PrintBackspace -##############
- REM Clears screen at [--B]
- REM NOTE: Doesn't effect @NextPrint
- REM #################################
- PrintBackspace:
- SET PUSH, A
- SET [B], 0x1920
- REM # Check for start of line
- SET A, [@NextLine]
- ADD A, 3
- IFL B, A
- MOV PrintBackspace_skip
- SUB B, 1
- SET [B], 0x1920
- PrintBackspace_skip:
- SET [B], 0x199D
- SET A, POP
- SET PC, POP
- REM #- ClearScreen -#################
- REM Clears the screen to 0x1920
- REM #################################
- ClearScreen:
- PUSHA
- SET A, [@ScreenStart]
- SET B, [@ScreenEnd]
- ADD B, 1
- SET [@NextLine], 0x7FE0
- SET [@NextPrint], 0x7FE0
- ClearScreen_loop:
- SET [A], 0x1920
- ADD A, 1
- IFL A, B
- MOV ClearScreen_loop
- POPA
- SET PC, POP
- REM #- TrimStr -#####################
- REM Trims tailing white space from
- REM null-string at [A]
- REM #################################
- TrimStr:
- SET PUSH, A
- TrimStr_findend:
- IFE [A], 0
- MOV TrimStr_trimspace
- ADD A, 1
- MOV TrimStr_findend
- TrimStr_trimspace:
- REM # Back from null
- SUB A, 1
- TrimStr_trimspace_loop:
- IFN [A], 0x20
- MOV TrimStr_exit
- SUB A, 1
- MOV TrimStr_trimspace_loop
- TrimStr_exit:
- SET [A + 1], 0
- SET A, POP
- SET PC, POP
- REM #- StrEquals -###################
- REM If string [A] == string [B] {
- REM JSR to C and after
- REM return to I if I != 0
- REM } else { do nothing }
- REM #################################
- StrEquals:
- PUSHA
- StrEquals_loop:
- IFN [A], [B]
- MOV StrEquals_nomatch
- IFE [A], 0
- IFE [B], 0
- MOV StrEquals_match
- ADD A, 1
- ADD B, 1
- MOV StrEquals_loop
- StrEquals_match:
- JSR C
- IFE I, 0
- MOV StrEquals_nomatch
- POPA
- REM # Clear PC from stack
- SET [@ClearStack], POP
- SET PC, I
- REM # No match found
- StrEquals_nomatch:
- POPA
- SET PC, POP
- REM #- WaitForNumber -################
- REM Wait for a hex number with then
- REM format #### to be entered
- REM ##################################
- WaitForNumber:
- SET PUSH, B
- SET PUSH, C
- REM # Load line to B
- JSR LoadNextLine
- REM # %
- SET [B], 0x1925
- ADD B, 2
- SET [B], 0x199D
- SET C, &InputNumber
- JSR SetKeyCallback
- SET C, POP
- SET B, POP
- SET PC, POP
- REM ------------------------
- REM # Input line CALLBACK
- REM ------------------------
- &InputNumber:
- SET PUSH, C
- SET A, 1
- HWI 1
- IFE C, 0x10
- JSR PrintBackspace
- IFE C, 0x11
- MOV &InputNumber_done
- REM # Shorter char range check
- SET A, [@NextLine]
- ADD A, 0x5
- IFG B, A
- MOV &InputNumber_exit
- REM # 0-9
- IFG C, 0x2F
- IFL C, 0x3A
- JSR PrintChar
- REM # a-f
- IFG C, 0x60
- IFL C, 0x67
- JSR PrintChar
- REM # Not done yet
- MOV &InputNumber_exit
- &InputNumber_done:
- SET [B], 0x1920
- JSR ClearKeyCallback
- &InputNumber_exit:
- SET C, POP
- SET PC, POP
- REM #- ParseNumber -#################
- REM Parses a number stored in @Input
- REM and stores the result in C
- REM If error I == 0xffff
- REM #################################
- ParseNumber:
- SET PUSH, A
- SET PUSH, B
- SET C, 0
- SET I, 12
- SET A, @Input
- ParseNumber_loop:
- SET B, [A]
- REM # 0-9
- IFL B, 0x3A
- SUB B, 0x30
- REM # a-f
- IFG B, 0x60
- IFL B, 0x67
- SUB B, 0x57
- IFG B, 0xF
- MOV ParseNumber_bad
- REM # Add to number
- SHL B, I
- ADD C, B
- ADD A, 1
- SUB I, 4
- REM # IF > -1
- IFA I, 0xffff
- MOV ParseNumber_loop
- REM # Is a 4 char number
- IFE [A], 0x20
- MOV ParseNumber_done
- IFE [A], 0x00
- MOV ParseNumber_done
- REM # Bad input
- ParseNumber_bad:
- SET I, 0xffff
- SET A, $InvalidInput
- JSR PrintLn
- ParseNumber_done:
- SET B, POP
- SET A, POP
- SET PC, POP
- REM #- WaitForString -################
- REM Wait for a command to be entered
- REM ##################################
- WaitForString:
- SET PUSH, B
- SET PUSH, C
- REM # Load line to B
- JSR LoadNextLine
- REM # >
- SET [B], [@CursorChar]
- ADD B, 2
- SET [B], 0x199D
- SET C, &InputLine
- JSR SetKeyCallback
- SET C, POP
- SET B, POP
- SET PC, POP
- REM # Input line CALLBACK
- &InputLine:
- SET PUSH, C
- SET A, 1
- HWI 1
- IFE C, 0x10
- JSR PrintBackspace
- IFE C, 0x11
- MOV &InputLine_done
- IFG C, 0x1F
- IFL C, 0x7F
- JSR PrintChar
- REM # Not done
- MOV &InputLine_exit
- &InputLine_done:
- SET [B], 0x1900
- JSR ClearKeyCallback
- JSR ParseLineInput
- &InputLine_exit:
- SET C, POP
- SET PC, POP
- REM #- ParseLineInput -##############
- REM Parses the input out of this line
- REM #################################
- ParseLineInput:
- PUSHA
- SET A, @Input
- SET B, [@NextLine]
- ADD B, 2
- SET C, B
- ADD C, 28
- ParseLineInput_loop:
- IFE [B], 0x1900
- MOV ParseLineInput_end
- IFG B, C
- MOV ParseLineInput_end
- SET [A], [B]
- AND [A], 0x00ff
- ADD A, 1
- ADD B, 1
- MOV ParseLineInput_loop
- ParseLineInput_end:
- REM # End with null
- SET [A], 0
- POPA
- SET PC, POP
- REM #- SendWordPacket -##############
- REM Send a packet to the IP 'XL', at
- REM port 'XH' with data 'YL'. Calls
- REM to hander 'YH' if reply
- REM #################################
- SendWordPacket:
- PUSHA
- REM # Open ping port 'XH'
- SET A, 0
- SET B, XH
- HWI 4
- REM # Set target to 'XL':'XH'
- SET A, 3
- SET B, XL
- SET C, XH
- HWI 4
- REM # Append data 'YL'
- SET A, 4
- SET B, YL
- HWI 4
- REM # Set time-out for 3 seconds
- SET A, 3
- SET B, SendWordPacket_Timeout
- JSR SetTimeout
- REM # Send packet
- SET A, 5
- HWI 4
- REM # Listen with callback 'YH'
- SET C, YH
- JSR SetPacketCallback
- REM # Wait until handler cleared
- SendWordPacket_wait:
- IFN [@PacketHandler], 0
- SET PC, SendWordPacket_wait
- SendWordPacket_exit:
- REM # Close port
- SET A, 1
- SET B, XH
- HWI 4
- POPA
- SET PC, POP
- REM -----------------------------
- REM # Packet timeout HANDLER
- REM # if timeout pick-up here
- REM -----------------------------
- SendWordPacket_Timeout:
- JSR ClearPacketCallback
- SET A, $Failed
- JSR PrintLn
- MOV SendWordPacket_exit
- REM #- SetKeyCallback -###############
- REM Set an event for a keyboard event
- REM Will MOV to C when event triggers
- REM Hangs until @KeyHandler == 0
- REM ##################################
- SetKeyCallback:
- SET PUSH, A
- SET PUSH, B
- SET A, 3
- SET B, C
- SET [@KeyHandler], B
- HWI 1
- SET B, POP
- SET A, POP
- REM # Wait for the state to update
- SetKeyCallback_wait:
- IFN [@KeyHandler], 0
- MOV SetKeyCallback_wait
- SET PC, POP
- REM #- ClearKeyCallback -#############
- REM Clears a KeyCallback
- REM ##################################
- ClearKeyCallback:
- SET PUSH, A
- SET PUSH, B
- SET [@KeyHandler], 0x0000
- SET A, 0
- HWI 1
- SET A, 3
- SET B, 0
- HWI 1
- SET A, POP
- SET B, POP
- SET PC, POP
- REM #- SetPacketCallback -############
- REM Set event for receiving a packet
- REM Will MOV to C when event triggers
- REM ##################################
- SetPacketCallback:
- SET PUSH, A
- SET PUSH, B
- SET A, 0xC
- SET B, C
- SET [@PacketHandler], B
- HWI 4
- SET B, POP
- SET A, POP
- SET PC, POP
- REM #- ClearPacketCallback -##########
- REM Clears a PacketCallback
- REM ##################################
- ClearPacketCallback:
- SET PUSH, A
- SET PUSH, B
- SET [@PacketHandler], 0x0000
- REM # Clear packet buffer
- SET A, 0xB
- HWI 4
- REM # Clear message
- SET A, 0xC
- SET B, 0
- HWI 4
- SET A, POP
- SET B, POP
- SET PC, POP
- REM #- SetTimeout -###################
- REM Starts a timeout which will MOV
- REM to B after A seconds
- REM * NOTE:
- REM Timeout is not a handler,
- REM will permanently move to the
- REM timeout address
- REM ##################################
- SetTimeout:
- SET PUSH, A
- SET PUSH, B
- SET [@TimeoutTicks], A
- SET [@TimeoutAddress], B
- REM # Int with message B
- SET A, 2
- HWI 2
- REM # Start timer
- SET A, 0
- SET B, 60
- HWI 2
- SET B, POP
- SET A, POP
- SET PC, POP
- REM #- DoTimeout -####################
- REM Checks for timeout completion, if
- REM done moves to the timeout address
- REM ##################################
- DoTimeout:
- SET PUSH, C
- REM # Get ticks
- SET A, 1
- HWI 2
- REM # If less than timeout ticks skip
- IFL C, [@TimeoutTicks]
- MOV DoTimeout_notyet
- SET C, POP
- REM # Clear the timeout
- SET [@HandlerReg], [@TimeoutAddress]
- JSR ClearTimeout
- REM # Reset from JSR
- SET [@ClearStack], POP
- REM # Reset from interrupt
- SET A, POP
- SET [@ClearStack], POP
- SET [@ClearStack], POP
- IAQ 0
- REM # Move to the handler
- SET PC, [@HandlerReg]
- DoTimeout_notyet:
- SET C, POP
- SET PC, POP
- REM #- ClearTimeout -#################
- REM Clears the current timeout
- REM ##################################
- ClearTimeout:
- SET PUSH, A
- SET PUSH, B
- SET [@TimeoutAddress], 0
- REM # Stop timer
- SET A, 0
- SET B, 0
- HWI 2
- REM # Clear message
- SET A, 2
- SET B, 0
- HWI 2
- SET B, POP
- SET A, POP
- SET PC, POP
- REM #- HardwareInterrupt -############
- REM The hardware interrupt handler
- REM ##################################
- HardwareIterrupt:
- IFE A, 0
- RFI 0x0
- REM # Event handlers
- IFE A, [@KeyHandler]
- JSR [@KeyHandler]
- IFE A, [@PacketHandler]
- JSR [@PacketHandler]
- REM # Insertion handlers
- IFE A, [@TimeoutAddress]
- JSR DoTimeout
- RFI 0x0
- FILL NETLIB, 1024
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement