<commit_before>' by: <NAME> - <EMAIL>

con

  TARGET_ID_CODE_1 = $2BA0_1477
  CTRL_STAT_WRITE_VALUE = $5400_0000
  CTRL_STAT_READ_VALUE = $F400_0000
  CTRL_STAT_READ_MASK = $FF00_0000
  TARGET_ID_CODE_2 = $2477_0011

  HALT_ADDRESS = $E000_EDF0
  HALT_VALUE = $A05F_0003

  FLASH_UNLOCK_ADDRESS = $4002_3C0C
  FLASH_UNLOCK_KEY_1 = <KEY>
  FLASH_UNLOCK_KEY_2 = $0203_0405

  PROGRAM_UNLOCK_ADDRESS = $4002_3C10
  PROGRAM_UNLOCK_KEY_1 = <KEY>
  PROGRAM_UNLOCK_KEY_2 = $1314_1516

  DATA_ADDRESS = $08000000
  
var

  long cog_id, swd_action, data_block[128] ' Do Not Rearrange!
  
pub internal_block_address

  return @data_block

pub write_block

  if swd_action == "E"
    abort 1
  
  swd_action := "W"

pub read_block

  if swd_action == "E"
    abort 1

  swd_action := "R"

pub busy

  if swd_action == "E"
    abort 1

  return swd_action

pub start(io_pin, clk_pin, reset_pin)

  swdio_pin := |<io_pin
  swdclk_pin := |<clk_pin
  swd_action := 0

  outa[reset_pin] := 0 
  dira[reset_pin] := 1
  waitcnt((clkfreq / 100) + cnt)
  dira[reset_pin] := 0

  cog_id := cognew(@cog_addr, @swd_action) + 1

pub stop(reset_pin)

  if cog_id
    cogstop(cog_id~ -1)

  outa[reset_pin] := 0 
  dira[reset_pin] := 1
  waitcnt((clkfreq / 100) + cnt)
  dira[reset_pin] := 0

dat

' /////////// Init

cog_addr      mov action_addr, par
              mov block_addr, par
              add block_addr, #4            

              or dira, swdio_pin
              or dira, swdclk_pin  

' /////////// Main

loop          rdlong par, action_addr
              cmp par, #"W" wz
if_z          call #do_w

              rdlong par, action_addr
              cmp par, #"R" wz
if_z          call #do_r

              jmp #loop

' /////////// Write Logic

do_w          cmp first_write, #1 wz
if_nz         mov first_write, #1
if_nz         call #w_init

              mov par, #128
              mov c, block_addr 
do_w_l        mov a, write_ptr
              rdlong b, c
              call #ahb_w
              add write_ptr, #4
              add c, #4
              djnz par, #do_w_l 

do_w_ret      ret
first_write   long 0
write_ptr     long DATA_ADDRESS

' /////////// Read Logic

do_r          cmp first_read, #1 wz
if_nz         mov first_read, #1
if_nz         call #r_init

              mov par, #128
              mov c, block_addr 
do_r_l        mov a, read_ptr
              call #ahb_r
              wrlong b, c
              add read_ptr, #4
              add c, #4
              djnz par, #do_r_l

do_r_ret      ret
first_read    long 0
read_ptr      long DATA_ADDRESS 

' /////////// Sub Write Init

w_init        call #send_res
              call #send_seq
              call #send_res

              ' read id 1
              
              mov x, #%0100
              call #send_req

              cmp y, init_id_code1 wz
if_nz         jmp #fatal_error

              ' power up req

              mov x, #%0010
              mov y, ctrl_stat_w
              call #send_req

              ' power up ack
              
              mov x, #%0110
              call #send_req

              and y, ctrl_stat_rm
              cmp y, ctrl_stat_r wz
if_nz         jmp #fatal_error

              ' set select

              mov x, #%0001
              mov y, #$F0
              call #send_req

              ' read id 2

              mov x, #%1111
              call #send_req

              cmp y, init_id_code2 wz
if_nz         jmp #fatal_error

              ' set select

              mov x, #%0001
              mov y, #0
              call #send_req

              ' setup control (32-bit auto-incrementing)

              mov x, #%1100
              call #send_req

              mov x, #%1000
              andn y, #$FF
              or y, #$12
              call #send_req
              
              ' halt processor

              mov a, halt_a
              mov b, halt_v
              call #ahb_w

              ' unlock flash

              mov a, flash_key_a
              mov b, flash_key_1
              call #ahb_w

              mov a, flash_key_a
              mov b, flash_key_2
              call #ahb_w

              ' unlock program

              mov a, program_key_a
              mov b, program_key_1
              call #ahb_w

              mov a, program_key_a
              mov b, program_key_2
              call #ahb_w             

w_init_ret    ret

init_id_code1 long TARGET_ID_CODE_1  
ctrl_stat_w   long CTRL_STAT_WRITE_VALUE
ctrl_stat_r   long CTRL_STAT_READ_VALUE
ctrl_stat_rm  long CTRL_STAT_READ_MASK
init_id_code2 long TARGET_ID_CODE_2

halt_a        long HALT_ADDRESS
halt_v        long HALT_VALUE

flash_key_a   long FLASH_UNLOCK_ADDRESS
flash_key_1   long FLASH_UNLOCK_KEY_1
flash_key_2   long FLASH_UNLOCK_KEY_2

program_key_a long PROGRAM_UNLOCK_ADDRESS
program_key_1 long PROGRAM_UNLOCK_KEY_1
program_key_2 long PROGRAM_UNLOCK_KEY_1

' /////////// Sub Read Init

r_init

r_init_ret    ret

' /////////// Sub AHB Write
' IN a = Address (32-bits)
' IN b = Data (32-bits)

ahb_w         mov x, #%1010 ' AP-TAR Write 
              mov y, a
              call #send_req

              mov x, #%1011 ' AP-DRW Write
              mov y, b
              call #send_req

              mov x, #%0111 ' DP-RDBUFF Read
              call #send_req

ahb_w_ret     ret

' /////////// Sub AHB Read
' IN a = Address (32-bits)
' OUT b = Data (32-bits)

ahb_r         mov x, #%1010 ' AP-TAR Write 
              mov y, a
              call #send_req

              mov x, #%1111 ' AP-DRW Read
              call #send_req

              mov x, #%0111 ' DP-RDBUFF Read
              call #send_req
              mov b, y

ahb_r_ret     ret

' /////////// Sub Send Reset

send_res      mov i, #100
              or outa, swdio_pin 
send_res_l    xor outa, swdclk_pin    
              djnz i, #send_res_l
              andn outa, swdio_pin 
send_res_ret  ret

' /////////// Sub Send (JTAG-to-SWD) Seq

send_seq      mov i, #16<commit_msg>Ready to test<commit_after>' by: <NAME> - <EMAIL>

con

  TARGET_ID_CODE_1 = $2BA0_1477
  CTRL_STAT_WRITE_VALUE = $5000_0000
  CTRL_STAT_READ_VALUE = $F000_0000
  CTRL_STAT_READ_MASK = $F000_0000
  TARGET_ID_CODE_2 = $2477_0011

  HALT_ADDRESS = $E000_EDF0
  HALT_VALUE = $A05F_0003
  HALT_2_ADDRESS = $E000_EDFC
  HALT_2_VALUE = $0000_0001
  HALT_3_ADDRESS = $E000_ED0C
  HALT_3_VALUE = $FA05_0004
  
  FLASH_UNLOCK_ADDRESS = $4002_3C04
  FLASH_UNLOCK_KEY_1 = $4567_0123
  FLASH_UNLOCK_KEY_2 = $CDEF_89AB

  MASS_ERASE_ADDRESS = $4002_3C10
  MASS_ERASE_VALUE = $0001_8206

  MASS_ERASE_STATUS_ADDRESS = $4002_3C0C
  MASS_ERASE_STATUS_VALUE = $0001_0000
  MASS_ERASE_STATUS_MASK = $0001_0000
  
  DATA_ADDRESS = $08000000
  
var

  long cog_id, swd_action, data_block[128] ' Do Not Rearrange!
  
pub internal_block_address

  return @data_block

pub write_block

  if swd_action == "E"
    abort 1
  
  swd_action := "W"

pub read_block

  if swd_action == "E"
    abort 1

  swd_action := "R"

pub busy

  if swd_action == "E"
    abort 1

  return swd_action

pub start(io_pin, clk_pin, reset_pin)

  swdio_pin := |<io_pin
  swdclk_pin := |<clk_pin
  swd_action := 0

  outa[reset_pin] := 0 
  dira[reset_pin] := 1
  waitcnt((clkfreq / 100) + cnt)
  dira[reset_pin] := 0

  cog_id := cognew(@cog_addr, @swd_action) + 1

pub stop(reset_pin)

  if cog_id
    cogstop(cog_id~ -1)

  outa[reset_pin] := 0 
  dira[reset_pin] := 1
  waitcnt((clkfreq / 100) + cnt)
  dira[reset_pin] := 0

dat

' /////////// Init

cog_addr      mov action_addr, par
              mov block_addr, par
              add block_addr, #4            

              or dira, swdio_pin
              or dira, swdclk_pin  

' /////////// Main

loop          rdlong par, action_addr
              cmp par, #"W" wz
if_z          call #do_w

              rdlong par, action_addr
              cmp par, #"R" wz
if_z          call #do_r

              jmp #loop

' /////////// Write Logic

do_w          cmp first_write, #1 wz
if_nz         mov first_write, #1
if_nz         call #w_init

              mov par, #128
              mov c, block_addr 
do_w_l        mov a, write_ptr
              rdlong b, c
              call #ahb_w
              add write_ptr, #4
              add c, #4
              djnz par, #do_w_l 

do_w_ret      ret
first_write   long 0
write_ptr     long DATA_ADDRESS

' /////////// Read Logic

do_r          cmp first_read, #1 wz
if_nz         mov first_read, #1
if_nz         call #r_init

              mov par, #128
              mov c, block_addr 
do_r_l        mov a, read_ptr
              call #ahb_r
              wrlong b, c
              add read_ptr, #4
              add c, #4
              djnz par, #do_r_l

do_r_ret      ret
first_read    long 0
read_ptr      long DATA_ADDRESS 

' /////////// Sub Write Init

w_init        call #send_res
              call #send_seq
              call #send_res

              ' read id 1
              
              mov x, #%0100
              call #send_req

              cmp y, init_id_code1 wz
if_nz         jmp #fatal_error

              ' power up req

              mov x, #%0010
              mov y, ctrl_stat_w
              call #send_req

              ' power up ack
              
              mov x, #%0110
              call #send_req

              and y, ctrl_stat_rm
              cmp y, ctrl_stat_r wz
if_nz         jmp #fatal_error

              ' set select

              mov x, #%0001
              mov y, #$F0
              call #send_req

              ' read id 2

              mov x, #%1111
              call #send_req

              cmp y, init_id_code2 wz
if_nz         jmp #fatal_error

              ' set select

              mov x, #%0001
              mov y, #0
              call #send_req

              ' setup control

              mov x, #%1100
              call #send_req

              mov x, #%1000
              andn y, #$07
              or y, #$02
              call #send_req
              
              ' halt processor

              mov a, halt_a
              mov b, halt_v
              call #ahb_w

              mov a, halt_2_a
              mov b, halt_2_v
              call #ahb_w

              mov a, halt_3_a
              mov b, halt_3_v
              call #ahb_w

              ' unlock flash

              mov a, flash_key_a
              mov b, flash_key_1
              call #ahb_w

              mov a, flash_key_a
              mov b, flash_key_2
              call #ahb_w

              ' erase flash

              mov a, flash_ctrl_a
              mov b, flash_ctrl_v
              call #ahb_w

              ' wait till done

w_init_l      mov a, flash_sts_a
              call #ahb_r

              and b, flash_sts_m
              cmp b, flash_sts_v wz
if_nz         jmp #w_init_l      

w_init_ret    ret

init_id_code1 long TARGET_ID_CODE_1  
ctrl_stat_w   long CTRL_STAT_WRITE_VALUE
ctrl_stat_r   long CTRL_STAT_READ_VALUE
ctrl_stat_rm  long CTRL_STAT_READ_MASK
init_id_code2 long TARGET_ID_CODE_2

halt_a        long HALT_ADDRESS
halt_v        long HALT_VALUE
halt_2_a      long HALT_2_ADDRESS
halt_2_v      long HALT_2_VALUE
halt_3_a      long HALT_3_ADDRESS
halt_3_v      long HALT_3_VALUE

flash_key_a   long FLASH_UNLOCK_ADDRESS
flash_key_1   long FLASH_UNLOCK_KEY_1
flash_key_2   long FLASH_UNLOCK_KEY_2

flash_ctrl_a  long MASS_ERASE_ADDRESS
flash_ctrl_v  long MASS_ERASE_VALUE

flash_sts_a   long MASS_ERASE_STATUS_ADDRESS
flash_sts_v   long MASS_ERASE_STATUS_VALUE
flash_sts_m   long MASS_ERASE_STATUS_MASK

' /////////// Sub Read Init

r_init

r_init_ret    ret

' /////////// Sub AHB Write
' IN a = Address (32-bits)
' IN b = Data (32-bits)

ahb_w         mov x, #%1010 ' AP-TAR Write 
              mov y, a
              call #send_req

              mov x, #%1011 ' AP-DRW Write
              mov y, b
              call #send_req

              mov x, #%0111 ' DP-RDBUFF Read
              call #send_req

ahb_w_ret     ret

' /////////// Sub AHB Read
' IN a = Address (32-bits)
' OUT b = Data (32-bits)

ahb_r         mov x, #%1010 ' AP-TAR Write 
              mov y, a
              call #send_req

              mov x, #%1111 ' AP-DRW Read
              call #send_req

              mov x, #%0111 ' DP-RDBUFF Read
              call #send_req
              mov b, y

ahb_r_ret     ret

' /////////// Sub Send Reset

send_res      mov i, #200
              or outa, swdio_pin 
send_res_l    xor outa, swdclk_pin    
              djnz i, #send_res_l
              andn outa, swdio_pin 
send_res_ret  ret

' /////////// Sub Send (JTAG-to-SWD) Seq

send_seq      mov i, #16
{'fraction_non_alphanumeric': 0.07425349428208386, 'fraction_numerical': 0.03946950444726811, 'mean_word_length': 1.3882040584107718, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5943451', 'n_tokens_mistral': 4859, 'n_tokens_neox': 4391, 'n_words': 1172}