📄 mouse.asm
字号:
;
;*******************************************************
Endpoint0:
push X
push A
; flag that interrupt went off during processing.
inc [int_flag]
iord ep0_mode
and A, EP0_ACK
jz ep0_done
iord ep0_mode
asl A
jc ep0_setup_received
asl A
jc ep0_in_received
asl A
jc ep0_out_received
ep0_done:
pop A
pop X
reti
ep0_setup_received:
mov A, NAK_IN_OUT ; clear setup bit to enable
iowr ep0_mode ; writes to EP0 DMA buffer
mov A, [ep0_dmabuff + BMREQUESTTYPE] ; compact bmRequestType into 5 bit field
and A, E3h ; clear bits 4-3-2, these unused for our purposes
push A ; store value
asr A ; move bits 7-6-5 into 4-3-2's place
asr A
asr A
mov [int_temp], A ; store shifted value
pop A ; get original value
or A, [int_temp] ; or the two to get the 5-bit field
and A, 1Fh ; clear bits 7-6-5 (asr wraps bit7)
asl A ; shift to index jumptable
jacc bmRequestType_jumptable ; jump to handle bmRequestType
h2d_std_device:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc h2d_std_device_jumptable
h2d_std_interface:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc h2d_std_interface_jumptable
h2d_std_endpoint:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc h2d_std_endpoint_jumptable
h2d_class_interface:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc h2d_class_interface_jumptable
d2h_std_device:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc d2h_std_device_jumptable
d2h_std_interface:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc d2h_std_interface_jumptable
d2h_std_endpoint:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc d2h_std_endpoint_jumptable
d2h_class_interface:
mov A, [ep0_dmabuff + BREQUEST]
asl A
jacc d2h_class_interface_jumptable
;;************ DEVICE REQUESTS **************
clear_device_feature: ; CLEAR FEATURE
mov A, [ep0_dmabuff + WVALUELO]
cmp A, DEVICE_REMOTE_WAKEUP
jnz request_not_supported
mov A, 00h ; disable remote wakeup capability
mov [remote_wakeup], A
mov A, NO_CHANGE_PENDING
mov [ep0_in_flag], A
jmp initialize_no_data_control
set_device_feature: ; SET FEATURE
mov A, [ep0_dmabuff + WVALUELO]
cmp A, DEVICE_REMOTE_WAKEUP
jnz request_not_supported
mov A, FFh ; enable remote wakeup capability
mov [remote_wakeup], A
mov A, NO_CHANGE_PENDING
mov [ep0_in_flag], A
jmp initialize_no_data_control
set_device_address: ; SET ADDRESS
mov A, ADDRESS_CHANGE_PENDING ; set flag to indicate we
mov [ep0_in_flag], A ; need to change address on
mov A, [ep0_dmabuff + WVALUELO]
mov [pending_data], A
jmp initialize_no_data_control
set_device_configuration: ; SET CONFIGURATION
mov A, [ep0_dmabuff + WVALUELO]
cmp A, 01h
jz configure_device
unconfigure_device: ; set device as unconfigured
mov [configuration], A
mov A, DISABLE ; disable endpoint 1
iowr ep1_mode
mov A, EP0_INT ; turn off endpoint 1 interrupts
iowr endpoint_int
jmp set_device_configuration_done
configure_device: ; set device as configured
mov [configuration], A
mov A, [ep1_stall] ; if endpoint 1 is stalled
and A, FFh
jz ep1_nak_in_out
mov A, STALL_IN_OUT ; set endpoint 1 mode to stall
iowr ep1_mode
jmp ep1_set_int
ep1_nak_in_out:
mov A, NAK_IN_OUT ; otherwise set it to NAK in/out
iowr ep1_mode
ep1_set_int:
mov A, EP0_INT | EP1_INT ; enable endpoint 1 interrupts
iowr endpoint_int
mov A, 00h
mov [ep1_data_toggle], A ; reset the data toggle
mov [ep1_dmabuff0], A ; reset endpoint 1 fifo values
mov [ep1_dmabuff1], A
mov [ep1_dmabuff2], A
set_device_configuration_done:
mov A, NO_CHANGE_PENDING
mov [ep0_in_flag], A
jmp initialize_no_data_control
get_device_status: ; GET STATUS
mov A, DEVICE_STATUS_LENGTH
mov [maximum_data_count], A
mov A, [remote_wakeup] ; test remote wakeup status
and A, FFh
jz remote_wakeup_disabled
remote_wakeup_enabled: ; send remote wakeup enabled status
mov A, (device_status_wakeup_enabled - control_read_table)
mov [data_start], A
jmp initialize_control_read
remote_wakeup_disabled: ; send remote wakeup disabled status
mov A, (device_status_wakeup_disabled - control_read_table)
mov [data_start], A
jmp initialize_control_read
get_device_descriptor: ; GET DESCRIPTOR
mov A, [ep0_dmabuff + WVALUEHI]
asl A
jacc get_device_descriptor_jumptable
send_device_descriptor:
mov A, 00h
index device_desc_table
mov [maximum_data_count], A
mov A, (device_desc_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
send_configuration_descriptor:
mov A, 02h
index config_desc_table:
mov [maximum_data_count], A
mov A, (config_desc_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
send_string_descriptor:
mov A, [ep0_dmabuff + WVALUELO]
asl A
jacc string_jumptable:
language_string:
mov A, 00h
index ilanguage_string
mov [maximum_data_count], A
mov A, (ilanguage_string - control_read_table)
mov [data_start], A
jmp initialize_control_read
manufacturer_string:
mov A, 00h
index imanufacturer_string
mov [maximum_data_count], A
mov A, (imanufacturer_string - control_read_table)
mov [data_start], A
jmp initialize_control_read
product_string:
mov A, 00h
index iproduct_string
mov [maximum_data_count], A
mov A, (iproduct_string - control_read_table)
mov [data_start], A
jmp initialize_control_read
configuration_string:
mov A, 00h
index iconfiguration_string
mov [maximum_data_count], A
mov A, (iconfiguration_string - control_read_table)
mov [data_start], A
jmp initialize_control_read
send_interface_descriptor:
mov A, 00h
index interface_desc_table
mov [maximum_data_count], A
mov A, (interface_desc_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
send_endpoint_descriptor:
mov A, 00h
index endpoint_desc_table
mov [maximum_data_count], A
mov A, (endpoint_desc_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
get_device_configuration: ; GET CONFIGURATION
mov A, DEVICE_CONFIG_LENGTH
mov [maximum_data_count], A
mov A, [configuration] ; test configuration status
and A, FFh
jz device_unconfigured
device_configured: ; send configured status
mov A, (device_configured_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
device_unconfigured: ; send unconfigured status
mov A, (device_unconfigured_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
;;************ INTERFACE REQUESTS ***********
set_interface_interface: ; SET INTERFACE
mov A, [ep0_dmabuff + WVALUELO]
cmp A, 00h ; there are no alternate interfaces
jz alternate_supported ; for this device
alternate_not_supported: ; if the host requests any other
jmp request_not_supported ; alternate than 0, stall.
alternate_supported:
mov A, NO_CHANGE_PENDING
mov [ep0_in_flag], A
jmp initialize_no_data_control
get_interface_status: ; GET STATUS
mov A, INTERFACE_STATUS_LENGTH
mov [maximum_data_count], A
mov A, (interface_status_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
get_interface_interface: ; GET INTERFACE
mov A, INTERFACE_ALTERNATE_LENGTH
mov [maximum_data_count], A
mov A, (interface_alternate_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
set_interface_idle: ; SET IDLE
mov A, [ep0_dmabuff + WVALUEHI] ; test if new idle time
cmp A, 00h ; disables idle timer
jz idle_timer_disable
mov A, [idle_timer] ; test if less than 4ms left
cmp A, 01h
jz set_idle_last_not_expired
mov A, [ep0_dmabuff + WVALUEHI] ; test if time left less than
sub A, [idle_timer] ; new idle value
jnc set_idle_new_timer_less
jmp set_idle_normal
idle_timer_disable:
mov [idle], A ; disable idle timer
jmp set_idle_done
set_idle_last_not_expired:
mov A, EVENT_PENDING ; send report immediately
mov [event_machine], A
mov A, 00h ; reset idle prescaler
mov [idle_prescaler], A
mov A, [ep0_dmabuff + WVALUEHI] ; set new idle value
mov [idle_timer], A
mov [idle], A
jmp set_idle_done
set_idle_new_timer_less:
mov A, 00h
mov [idle_prescaler], A ; reset idle prescaler
mov A, [ep0_dmabuff + WVALUEHI]
mov [idle_timer], A ; update idle time value
mov [idle], A
jmp set_idle_done
set_idle_normal:
mov A, 00h ; reset idle prescaler
mov [idle_prescaler], A
mov A, [ep0_dmabuff + WVALUEHI] ; update idle time value
mov [idle_timer], A
mov [idle], A
set_idle_done:
mov A, NO_CHANGE_PENDING ; respond with no-data control
mov [ep0_in_flag], A ; transaction
jmp initialize_no_data_control
set_interface_protocol: ; SET PROTOCOL
mov A, [ep0_dmabuff + WVALUELO]
mov [protocol], A ; set protocol value
mov A, NO_CHANGE_PENDING
mov [ep0_in_flag], A ; respond with no-data control
jmp initialize_no_data_control ; transaction
get_interface_report: ; GET REPORT
mov A, DATA_TOGGLE ; set data toggle to DATA ONE
mov [ep0_data_toggle], A
mov A, NAK_IN_OUT ; clear setup bit to write to
iowr ep0_mode ; endpoint fifo
mov A, [ep1_dmabuff0] ; copy over button data
mov [ep0_dmabuff0], A
mov A, [ep1_dmabuff1] ; copy horizontal data
mov [ep0_dmabuff1], A
mov A, [ep1_dmabuff2] ; copy vertical data
mov [ep0_dmabuff2], A
mov A, TRANS_CONTROL_READ
mov [ep0_transtype], A
mov A, CONTROL_READ_DATA ; set state machine state
mov [ep0_in_machine], A
mov X, 03h ; set number of byte to transfer to 3
jmp dmabuffer_load_done ; jump to finish transfer
get_interface_idle: ; GET IDLE
mov A, DATA_TOGGLE ; set data toggle to DATA ONE
mov [ep0_data_toggle], A
mov A, NAK_IN_OUT ; clear setup bit to write to
iowr ep0_mode ; endpoint fifo
mov A, [idle] ; copy over idle time
mov [ep0_dmabuff0], A
mov A, TRANS_CONTROL_READ
mov [ep0_transtype], A
mov A, CONTROL_READ_DATA ; set state machine state
mov [ep0_in_machine], A
mov X, 01h ; set number of byte to transfer to 3
jmp dmabuffer_load_done ; jump to finish transfer
get_interface_protocol: ; GET PROTOCOL
mov A, INTERFACE_PROTOCOL_LENGTH
mov [maximum_data_count], A ; get offset of device descriptor table
mov A, [protocol]
and A, 01h
jz boot_protocol
report_protocol:
mov A, (interface_report_protocol - control_read_table)
mov [data_start], A
jmp initialize_control_read ; get ready to send data
boot_protocol:
mov A, (interface_boot_protocol - control_read_table)
mov [data_start], A
jmp initialize_control_read ; get ready to send data
get_interface_hid: ; GET HID REPORT DESCRIPTOR
mov A, [ep0_dmabuff + WVALUEHI]
cmp A, 22h ; test if report request
jz get_hid_report_desc
cmp A, 21h ; test if class request
jz get_hid_desc
jmp request_not_supported
get_hid_desc:
mov A, 00h
index hid_desc_table
mov [maximum_data_count], A ; get offset of device descriptor table
mov A, (hid_desc_table - control_read_table)
mov [data_start], A
jmp initialize_control_read ; get ready to send data
get_hid_report_desc:
mov A, 07h
index hid_desc_table
mov [maximum_data_count], A ; get offset of device descriptor table
mov A, (hid_report_desc_table - control_read_table)
mov [data_start], A
jmp initialize_control_read ; get ready to send data
;;************ ENDPOINT REQUESTS ************
clear_endpoint_feature: ; CLEAR FEATURE
mov A, [ep0_dmabuff + WVALUELO]
cmp A, ENDPOINT_STALL
jnz request_not_supported
mov A, 00h ; clear endpoint 1 stall
mov [ep1_stall], A
mov A, NO_CHANGE_PENDING ; respond with no-data control
mov [ep0_in_flag], A
jmp initialize_no_data_control
set_endpoint_feature: ; SET FEATURE
mov A, [ep0_dmabuff + WVALUELO]
cmp A, ENDPOINT_STALL
jnz request_not_supported
mov A, FFh ; stall endpoint 1
mov [ep1_stall], A
mov A, NO_CHANGE_PENDING ; respond with no-data control
mov [ep0_in_flag], A
jmp initialize_no_data_control
get_endpoint_status: ; GET STATUS
mov A, ENDPOINT_STALL_LENGTH
mov [maximum_data_count], A
mov A, [ep1_stall] ; test if endpoint 1 stalled
and A, FFh
jnz endpoint_stalled
endpoint_not_stalled: ; send no-stall status
mov A, (endpoint_nostall_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
endpoint_stalled: ; send stall status
mov A, (endpoint_stall_table - control_read_table)
mov [data_start], A
jmp initialize_control_read
;;***************** CONTROL READ TRANSACTION **************
initialize_control_read:
mov A, TRANS_CONTROL_READ ; set transaction type to control read
mov [ep0_transtype], A
mov A, DATA_TOGGLE ; set data toggle to DATA ONE
mov [ep0_data_toggle], A
; if wLengthhi == 0
mov A, [ep0_dmabuff + WLENGTHHI] ; find lesser of requested and maximum
cmp A, 00h
jnz initialize_control_read_done
; and wLengthlo < maximum_data_count
mov A, [ep0_dmabuff + WLENGTHLO] ; find lesser of requested and maximum
cmp A, [maximum_data_count] ; response lengths
jnc initialize_control_read_done
; then maximum_data_count >= wLengthlo
mov A, [ep0_dmabuff + WLENGTHLO]
mov [maximum_data_count], A
initialize_control_read_done:
jmp control_read_data_stage ; send first packet
;;***************** CONTROL WRITE TRANSACTION *************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -