Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REM ================================
- REM * NETLIB v1.1
- REM The base library for NET code
- REM ================================
- NETLIB:
- REM # Init
- SET A, 0
- SET B, [@ScreenStart]
- HWI 0
- JSR ClearScreen
- IAS HardwareIterrupt
- SET PC, [@CodeStart]
- REM ##############
- REM NETLIB Vars
- REM ##############
- @CodeStart: DAT NETServ
- @ScreenStart: DAT 0x8000
- @NextPrint: DAT 0x7FE0
- @ScreenEnd: DAT 0x817F
- @PacketHandler: DAT 0x0000
- @KeyHandler: DAT 0x0000
- @TimeoutAddress: DAT 0x0000
- @TimeoutTicks: DAT 0x0000
- @HandlerReg: DAT 0x0000
- @IP: DAT 0x0000
- @ClearPC: 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
- $InvalidInput:
- DAT "Invalid input", 0x0
- REM ###############################
- 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 ###############################
- REM Print null-str at [A] on a
- REM new line
- REM ###############################
- PrintLn:
- SET PUSH, B
- REM # Load line to B
- JSR LoadNextLine
- JSR PrintStr
- SET B, POP
- SET PC, POP
- REM #################################
- REM Loads the address of the next
- REM line into B
- REM #################################
- LoadNextLine:
- ADD [@NextPrint], 0x20
- IFG [@NextPrint], [@ScreenEnd]
- JSR ScrollUp
- SET B, [@NextPrint]
- SET PC, POP
- REM ###############################
- REM Print null-str at [A]
- REM ###############################
- PrintStr:
- SET PUSH, C
- PrintStr_loop:
- IFE [A], 0
- MOV PrintStr_exit
- SET C, [A]
- BOR C, 0x1900
- SET [B], C
- ADD A, 1
- ADD B, 1
- MOV PrintStr_loop
- PrintStr_exit:
- SET C, POP
- SET PC, POP
- REM ###############################
- 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 #################################
- REM Print a character C to [B++]
- REM #################################
- PrintChar:
- SET PUSH, A
- REM # Check for end of line
- SET A, [@NextPrint]
- 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 ##################################
- REM Clears screen at [--B]
- REM ##################################
- PrintBackspace:
- SET PUSH, A
- SET [B], 0x1920
- REM # Check for start of line
- SET A, [@NextPrint]
- 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 #################################
- REM Clears the screen to 0x1920
- REM #################################
- ClearScreen:
- SET PUSH, A
- SET PUSH, B
- SET A, [@ScreenStart]
- SET B, [@ScreenEnd]
- ADD B, 1
- ClearScreen_loop:
- SET [A], 0x1920
- ADD A, 1
- IFL A, B
- MOV ClearScreen_loop
- SET B, POP
- SET A, POP
- SET PC, POP
- REM #################################
- REM Scroll the screen up a line
- REM #################################
- ScrollUp:
- SET PUSH, C
- SET PUSH, I
- SET I, [@ScreenStart]
- SET C, [@ScreenEnd]
- SUB C, 0x1F
- 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 #################################
- REM If string [A] == string [B] then
- REM JSR to C and then
- 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 [@ClearPC], POP
- SET PC, I
- REM # No match found
- StrEquals_nomatch:
- POPA
- SET PC, POP
- REM ##################################
- 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], 0x193E
- ADD B, 2
- SET [B], 0x199D
- SET C, &InputNumber
- JSR SetKeyCallback
- SET C, POP
- SET B, POP
- SET PC, POP
- REM # Input line Callback
- &InputNumber:
- SET PUSH, A
- 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, [@NextPrint]
- 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 A, POP
- SET PC, POP
- REM #################################
- 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 ##################################
- 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], 0x193E
- 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, A
- SET PUSH, C
- SET A, 1
- HWI 1
- IFE C, 0x10
- JSR PrintBackspace
- IFE C, 0x11
- MOV &InputLine_done
- IFG C, 0x1F
- IFL C, 0x80
- JSR PrintChar
- REM # Not done
- MOV &InputLine_exit
- &InputLine_done:
- SET [B], 0x1920
- JSR ClearKeyCallback
- JSR ParseLineInput
- &InputLine_exit:
- SET C, POP
- SET A, POP
- SET PC, POP
- REM #################################
- REM Parses the input out of this line
- REM #################################
- ParseLineInput:
- SET PUSH, A
- SET PUSH, B
- SET A, @Input
- SET B, [@NextPrint]
- ADD B, 2
- ParseLineInput_loop:
- IFE [B], 0x1920
- 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
- SET B, POP
- SET A, POP
- SET PC, POP
- REM ##################################
- 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 ##################################
- 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 ##################################
- 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 ##################################
- 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 ##################################
- 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 ##################################
- 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 [@ClearPC], POP
- REM # Reset from interrupt
- SET A, POP
- SET [@ClearPC], POP
- IAQ 0
- REM # Move to the handler
- SET PC, [@HandlerReg]
- DoTimeout_notyet:
- SET C, POP
- SET PC, POP
- REM ##################################
- 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 ##################################
- 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, 512
- REM ===================================
- REM * NETServ v1.0
- REM Host a dedicated server
- REM ===================================
- NETServ:
- SET A, $NetServ
- JSR PrintLn
- SET A, $Connecting
- JSR PrintLn
- JSR WaitForIP
- REM # Print "Connected IP: #"
- SET B, [@NextPrint]
- SET A, $Connected
- JSR PrintStr
- SET A, [@IP]
- JSR PrintHex
- REM # Open ping port
- SET A, 0
- SET B, 64
- HWI 4
- REM # Listen for packet
- SET C, &ServerPacket
- JSR SetPacketCallback
- SET A, $ServerStarted
- JSR PrintLn
- Server_loop:
- SET PC, Server_loop
- NETServ_exit:
- HNG
- REM ##########################
- REM # Server packet handler
- REM ##########################
- &ServerPacket:
- REM # Prepare to read
- SET A, 6
- HWI 4
- REM # Check for ping packet
- SET A, 8
- REM # First word 0x0001
- HWI 4
- IFN B, 1
- MOV &ServerPacket_exit
- REM # Reached end
- HWI 4
- IFN B, 0
- MOV &ServerPacket_exit
- REM # Get sender IP
- SET A, 7
- HWI 4
- SET I, B
- REM # Set target
- SET B, I
- SET A, 3
- SET C, 64
- HWI 4
- REM # Append 2
- SET A, 4
- SET B, 2
- HWI 4
- REM # Send packet
- SET A, 5
- HWI 4
- REM # Print sender IP
- SET A, I
- JSR LoadNextLine
- JSR PrintHex
- &ServerPacket_exit:
- SET PC, POP
- REM ##############
- REM Strings
- REM < 32 chars
- REM ##############
- $NetServ:
- DAT "NETServ v1.0", 0x0
- $Connecting:
- DAT "Connecting...", 0x0
- $Connected:
- DAT "Connected IP: ", 0x0
- $ServerStarted:
- DAT "Server started", 0x0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement