watch t.registers.clock_buffer[0].out[0]
watch t.registers.clock_buffer[1].out[0]
watch t.registers.clock_buffer[2].out[0]
watch t.registers.clock_buffer[3].out[0]


system "echo '[0] start test'"
system "echo '----------------------------------------------------------'"

set-qdi-channel-neutral "t.in" 5
set-qdi-channel-neutral "t.out" 4
set t.data[0].d[0] 0
set t.data[0].d[1] 0
set t.data[1].d[0] 0
set t.data[1].d[1] 0
set t.dly_cfg[0] 1
set t.dly_cfg[1] 1
set t.dly_cfg[2] 1

set t.out.a 0
set t.out.v 0
cycle
set t.in.a 0
set Reset 0
cycle
assert-qdi-channel-neutral "t.in" 5
assert-qdi-channel-neutral "t.out" 4
mode run
cycle

# check delay config programming 
assert t.registers.clk_dly.s[0] 1
assert t.registers.clk_dly.s[1] 1

assert t.registers.ff[0].q 0
assert t.registers.ff[1].q 0
assert t.registers.ff[2].q 0
assert t.registers.ff[3].q 0
assert t.registers.ff[4].q 0
assert t.registers.ff[5].q 0
assert t.registers.ff[6].q 0
assert t.registers.ff[7].q 0

assert-qdi-channel-neutral "t.out" 4
assert t.data[0].d[0] 0
assert t.data[0].d[1] 0
assert t.data[1].d[0] 0
assert t.data[1].d[1] 0
cycle
system "echo '[1] reset completed'"
system "echo '----------------------------------------------------------'"

set-qdi-channel-valid "t.in" 5 3
# 3 -> 00011 -> writing mode, address 00, word 11
cycle

assert t.in.a 1
assert-qdi-channel-neutral "t.out" 4
assert t.registers._in_v_temp 1
set-qdi-channel-neutral "t.in" 5
cycle
assert t.registers._in_v_temp 0

assert t.registers.ff[0].q 1
assert t.registers.ff[1].q 1
assert t.registers.ff[2].q 0
assert t.registers.ff[3].q 0
assert t.registers.ff[4].q 0
assert t.registers.ff[5].q 0
assert t.registers.ff[6].q 0
assert t.registers.ff[7].q 0
assert t.in.v 0

set t.out.a 0
set t.out.v 0
assert t.in.a 0
cycle 
assert t.registers._clock_temp_inv 1

system "echo '[3] first writing done'"
system "echo '----------------------------------------------------------'"

# set-qdi-channel-valid "t.in" 5 16
# # 16 -> 10000 -> reading mode, address 00, word 00 (word doesnt needed here)
# cycle
# assert t.registers._clock_temp_inv 1

# assert t.registers.word_to_read_X[0].out[0] 1
# assert t.registers.word_to_read_X[0].out[1] 1
# assert t.registers.word_to_read_X[0].out[2] 1
# assert t.registers.word_to_read_X[0].out[3] 1

# assert-qdi-channel-valid "t.out" 4 3
# set t.out.v 1
# cycle
# set t.out.a 1
# assert t.registers._clock_temp_inv 1
# cycle
# assert t.in.a 1
# set-qdi-channel-neutral "t.in" 5
# assert t.registers._clock_temp_inv 1
# cycle
# assert t.registers.ff[0].q 1
# assert t.registers.ff[1].q 1
# assert t.registers.ff[2].q 0
# assert t.registers.ff[3].q 0
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[4] reading done'"
# system "echo '----------------------------------------------------------'"


# set-qdi-channel-valid "t.in" 5 7
# # 7 -> 00111 -> writing mode, address 01, word 11
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1

# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 1
# assert t.registers.ff[1].q 1
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 0
# assert t.registers.ff[5].q 0
# assert t.registers.ff[6].q 0
# assert t.registers.ff[7].q 0
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# cycle 
# assert t.registers._clock_temp_inv 1

# system "echo '[5] second writing done'"
# system "echo '----------------------------------------------------------'"

# set-qdi-channel-valid "t.in" 5 11
# # 11 -> 01011 -> writing mode, address 10, word 11
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 1
# assert t.registers.ff[1].q 1
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 0
# assert t.registers.ff[7].q 0
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[6] third writing done'"
# system "echo '----------------------------------------------------------'"

# set-qdi-channel-valid "t.in" 5 15
# # 15 -> 01111 -> writing mode, address 11, word 11
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 1
# assert t.registers.ff[1].q 1
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 1
# assert t.registers.ff[7].q 1
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[7] fourth writing done'"
# system "echo '----------------------------------------------------------'"

# set-qdi-channel-valid "t.in" 5 28
# # 28 -> 11100 -> reading mode, address 11, word 00 (word doesnt needed here)
# cycle
# assert t.registers._clock_temp_inv 1

# assert-qdi-channel-valid "t.out" 4 15
# set t.out.v 1
# cycle
# set t.out.a 1
# assert t.registers._clock_temp_inv 1
# cycle
# assert t.in.a 1
# set-qdi-channel-neutral "t.in" 5
# assert t.registers._clock_temp_inv 1
# cycle
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[8] 11 reading done'"
# system "echo '----------------------------------------------------------'"

# set-qdi-channel-valid "t.in" 5 20
# # 20 -> 10100 -> reading mode, address 01, word 00 (word doesnt needed here)
# cycle
# assert t.registers._clock_temp_inv 1

# assert-qdi-channel-valid "t.out" 4 7
# set t.out.v 1
# cycle
# set t.out.a 1
# assert t.registers._clock_temp_inv 1
# cycle
# assert t.in.a 1
# set-qdi-channel-neutral "t.in" 5
# assert t.registers._clock_temp_inv 1
# cycle
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[9] 01 reading done'"
# system "echo '----------------------------------------------------------'"

# set-qdi-channel-valid "t.in" 5 24
# # 24 -> 11000 -> reading mode, address 10, word 00 (word doesnt needed here)
# cycle
# assert t.registers._clock_temp_inv 1

# assert-qdi-channel-valid "t.out" 4 11
# set t.out.v 1
# cycle
# set t.out.a 1
# assert t.registers._clock_temp_inv 1
# cycle
# assert t.in.a 1
# set-qdi-channel-neutral "t.in" 5
# assert t.registers._clock_temp_inv 1
# cycle
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle

# system "echo '[8] 10 reading done'"
# system "echo '----------------------------------------------------------'"

# set-qdi-channel-valid "t.in" 5 13
# # 13 -> 01101 -> writing mode, address 11, word 01
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 1
# assert t.registers.ff[1].q 1
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 1
# assert t.registers.ff[7].q 1
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[9] Rewrite 11 to 01'"
# system "echo '----------------------------------------------------------'"


# set-qdi-channel-valid "t.in" 5 0
# # 13 -> 00000 -> writing mode, address 00, word 00
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 0
# assert t.registers.ff[1].q 0
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 1
# assert t.registers.ff[7].q 1
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[9] Rewrite 11 to 01'"
# system "echo '----------------------------------------------------------'"


# set-qdi-channel-valid "t.in" 5 0
# # 0 -> 00000 -> writing mode, address 00, word 00
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 0
# assert t.registers.ff[1].q 0
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 1
# assert t.registers.ff[7].q 0
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[9] Rewrite 11 to 01'"
# system "echo '----------------------------------------------------------'"


# set-qdi-channel-valid "t.in" 5 0
# # 0 -> 00000 -> writing mode, address 00, word 00
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 0
# assert t.registers.ff[1].q 0
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 1
# assert t.registers.ff[7].q 0
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[9] Rewrite 11 to 01'"
# system "echo '----------------------------------------------------------'"


# set-qdi-channel-valid "t.in" 5 0
# # 13 -> 00000 -> writing mode, address 00, word 00
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 0
# assert t.registers.ff[1].q 0
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 1
# assert t.registers.ff[7].q 0
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[9] Rewrite 11 to 01'"
# system "echo '----------------------------------------------------------'"


# set-qdi-channel-valid "t.in" 5 3
# # 13 -> 00011 -> writing mode, address 00, word 11
# cycle

# assert t.in.a 1
# assert-qdi-channel-neutral "t.out" 4
# assert t.registers._in_v_temp 1
# set-qdi-channel-neutral "t.in" 5
# cycle
# assert t.registers._in_v_temp 0

# assert t.registers.ff[0].q 1
# assert t.registers.ff[1].q 1
# assert t.registers.ff[2].q 1
# assert t.registers.ff[3].q 1
# assert t.registers.ff[4].q 1
# assert t.registers.ff[5].q 1
# assert t.registers.ff[6].q 1
# assert t.registers.ff[7].q 0
# assert t.in.v 0

# set t.out.a 0
# set t.out.v 0
# assert t.in.a 0
# cycle 

# system "echo '[9] Rewrite 11 to 01'"
# system "echo '----------------------------------------------------------'"



