mirror of
https://github.com/KrisKennaway/ii-vision.git
synced 2025-02-07 12:30:46 +00:00
commit
2dcd3dadab
140
player/main.s
140
player/main.s
@ -397,7 +397,7 @@ _op_header_hgr:
|
||||
; Y register has the high byte of the W5100 address pointer in the RX socket code, so we
|
||||
; can't trash this until we are ready to point back there.
|
||||
checkrecv:
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
LDA #<S0RXRSR ; 2 Socket 0 Received Size register
|
||||
STA WADRL ; 4
|
||||
@ -440,7 +440,7 @@ recv: ; 15 cycles so far
|
||||
; X will usually already be 0 from op_ack except during first frame when reading
|
||||
; header but reset it unconditionally
|
||||
LDX #$00 ; 2
|
||||
BIT TICK ; 4 ; 36
|
||||
STA TICK ; 4 ; 36
|
||||
|
||||
NOP ; 2
|
||||
STA dummy ; 4
|
||||
@ -464,7 +464,7 @@ op_nop:
|
||||
; - read 2 bytes from the stream as address of next opcode
|
||||
;
|
||||
; Each opcode has 6 cycles of padding, which is necessary to support reordering things to
|
||||
; get the second "BIT TICK" at the right cycle offset.
|
||||
; get the second "STA TICK" at the right cycle offset.
|
||||
;
|
||||
; Where possible we share code by JMPing to a common tail instruction sequence in one of the
|
||||
; earlier opcodes. This is critical for reducing code size enough to fit.
|
||||
@ -480,8 +480,8 @@ op_nop:
|
||||
.macro op_tick_4 page
|
||||
;4+(4)+2+4+4+4+5+4+5+4+5+4+5+4+4+4+4+3=73
|
||||
.ident (.concat ("op_tick_4_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
STA zpdummy ; 3
|
||||
@ -524,9 +524,9 @@ tickident page, 7
|
||||
.macro op_tick_6 page
|
||||
;4+(2+4)+3+4+4+5+4+5+4+5+4+5+4+4+4+5+3
|
||||
.ident (.concat ("op_tick_6_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
|
||||
@ -569,9 +569,9 @@ tickident page, 8
|
||||
.macro op_tick_8 page
|
||||
;4+(4+4)+3+3+55
|
||||
.ident (.concat ("op_tick_8_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_55")) ; 3 + 55
|
||||
@ -580,10 +580,10 @@ tickident page, 8
|
||||
.macro op_tick_10 page
|
||||
;4+(4+2+4)+3+56
|
||||
.ident (.concat ("op_tick_10_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_56")) ; 3 + 56
|
||||
.endmacro
|
||||
@ -591,10 +591,10 @@ tickident page, 8
|
||||
.macro op_tick_12 page
|
||||
;4+(4+4+4)+3+3+51
|
||||
.ident (.concat ("op_tick_12_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_51")) ; 3 + 51
|
||||
@ -603,11 +603,11 @@ tickident page, 8
|
||||
.macro op_tick_14 page
|
||||
;4+(4+4+2+4)+3+52
|
||||
.ident (.concat ("op_tick_14_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_52")) ; 3+52
|
||||
.endmacro
|
||||
@ -615,14 +615,14 @@ tickident page, 8
|
||||
.macro op_tick_16 page
|
||||
; 4+(4+4+4+4)+5+2+3+43
|
||||
.ident (.concat ("op_tick_16_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
; This temporarily violates X=0 invariant required by tick_6, but lets us share a
|
||||
; common opcode tail; otherwise we need a dummy 4-cycle opcode between the ticks, which
|
||||
; doesn't leave enough to JMP with.
|
||||
LDX WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA page << 8,x ; 5
|
||||
LDX #$00 ; 2 restore X=0 invariant
|
||||
@ -633,14 +633,14 @@ tickident page, 8
|
||||
.macro op_tick_18 page
|
||||
; 4 + (4+4+4+2+4)+5+5+2+2+4+5+4+5+4+4+4+4+3
|
||||
.ident (.concat ("op_tick_18_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
; lets us reorder the 5-cycle STA page << 8,y outside of tick loop.
|
||||
; This temporarily violates X=0 invariant required by tick_6
|
||||
LDX WDATA ; 4
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA page << 8,Y ; 5
|
||||
STA page << 8,X ; 5
|
||||
@ -667,12 +667,12 @@ tickident page, 8
|
||||
.macro op_tick_20 page
|
||||
;4+(4+4+5+3+4)+3+46=73
|
||||
.ident (.concat ("op_tick_20_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
STA zpdummy ; 3
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_46"))
|
||||
.endmacro
|
||||
@ -681,12 +681,12 @@ tickident page, 8
|
||||
.macro op_tick_22 page
|
||||
; 4+(4+4+5+4+4)+3+3+42
|
||||
.ident (.concat ("op_tick_22_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_42")) ; 3 + 42
|
||||
@ -695,13 +695,13 @@ tickident page, 8
|
||||
.macro op_tick_24 page
|
||||
;4+(4+4+5+4+3+4)+3+42
|
||||
.ident (.concat ("op_tick_24_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
STA zpdummy ; 3
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_42"))
|
||||
.endmacro
|
||||
@ -709,13 +709,13 @@ tickident page, 8
|
||||
.macro op_tick_26 page ; pattern repeats from op_tick_8
|
||||
; 4+(4+4+5+4+5+4)+3+37
|
||||
.ident (.concat ("op_tick_26_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
BIT TICK; 4
|
||||
STA TICK; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_37")) ; 3 + 37
|
||||
@ -724,14 +724,14 @@ tickident page, 8
|
||||
.macro op_tick_28 page ; pattern repeats from op_tick_10
|
||||
; 4+(4+2+4+5+4+5+4)+3+38
|
||||
.ident (.concat ("op_tick_28_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_38"))
|
||||
.endmacro
|
||||
@ -739,14 +739,14 @@ tickident page, 8
|
||||
.macro op_tick_30 page ; pattern repeats from op_tick_12
|
||||
;4+(4+4+5+4+5+4+4)+3+3+33
|
||||
.ident (.concat ("op_tick_30_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_33")) ; 3 + 33
|
||||
@ -755,7 +755,7 @@ tickident page, 8
|
||||
.macro op_tick_32 page ; pattern repeats from op_tick_14
|
||||
;4+(4+4+5+4+5+4+2+4)+3+34
|
||||
.ident (.concat ("op_tick_32_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -763,7 +763,7 @@ tickident page, 8
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_34"))
|
||||
.endmacro
|
||||
@ -771,7 +771,7 @@ tickident page, 8
|
||||
.macro op_tick_34 page ; pattern repeats from op_tick_16
|
||||
; 4+(4+4+5+4+5+4+4+4)+2+5+5+3+20
|
||||
.ident (.concat ("op_tick_34_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -779,7 +779,7 @@ tickident page, 8
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
LDX WDATA ; 4 ; allows reordering STA ...,X outside ticks
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA page << 8,Y ; 5
|
||||
STA page << 8,X ; 5
|
||||
@ -792,7 +792,7 @@ tickident page, 8
|
||||
.macro op_tick_36 page ; pattern repeats from op_tick_18
|
||||
;4+(4+4+5+4+5+4+4+2+4)+5+5+2+2+4+4+4+4+3
|
||||
.ident (.concat ("op_tick_36_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -801,7 +801,7 @@ tickident page, 8
|
||||
LDY WDATA ; 4
|
||||
LDX WDATA ; 4
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA page << 8,Y ; 5
|
||||
STA page << 8,X ; 5
|
||||
@ -820,7 +820,7 @@ tickident page, 8
|
||||
.macro op_tick_38 page ; pattern repeats from op_tick_20
|
||||
; 4 + (4+4+5+4+5+4+5+3+4)+3+28
|
||||
.ident (.concat ("op_tick_38_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -829,7 +829,7 @@ tickident page, 8
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
STA zpdummy ; 3
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_28")) ; 3 + 28
|
||||
.endmacro
|
||||
@ -838,7 +838,7 @@ tickident page, 8
|
||||
.macro op_tick_40 page ; pattern repeats from op_tick_22
|
||||
;4+(4+4+5+4+5+4+5+4+4)+3+3+24
|
||||
.ident (.concat ("op_tick_40_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -847,7 +847,7 @@ tickident page, 8
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_24"))
|
||||
@ -856,7 +856,7 @@ tickident page, 8
|
||||
.macro op_tick_42 page ; pattern repeats from op_tick_24
|
||||
;4+(4+4+5+4+5+4+5+4+3+4)+3+24
|
||||
.ident (.concat ("op_tick_42_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -866,7 +866,7 @@ tickident page, 8
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
STA zpdummy ; 3
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_24")) ; 3 + 24
|
||||
.endmacro
|
||||
@ -874,7 +874,7 @@ tickident page, 8
|
||||
.macro op_tick_44 page ; pattern repeats from op_tick_26
|
||||
; 4 + (4+4+5+4+5+4+5+4+5+4)+3+3+19
|
||||
.ident (.concat ("op_tick_44_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -884,7 +884,7 @@ tickident page, 8
|
||||
STA page << 8,Y ; 5
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
BIT TICK; 4
|
||||
STA TICK; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_19")) ; 3 + 19
|
||||
@ -893,7 +893,7 @@ tickident page, 8
|
||||
.macro op_tick_46 page ; pattern repeats from op_tick_28
|
||||
;4+(4+2+4+5+4+5+4+5+4+5+4)+3+20
|
||||
.ident (.concat ("op_tick_46_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -904,7 +904,7 @@ tickident page, 8
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_20"))
|
||||
.endmacro
|
||||
@ -912,7 +912,7 @@ tickident page, 8
|
||||
.macro op_tick_48 page ; pattern repeats from op_tick_30
|
||||
;4+(4+4+5+4+5+4+5+4+5+4+4)+3+3+15
|
||||
.ident (.concat ("op_tick_48_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -924,7 +924,7 @@ tickident page, 8
|
||||
STA page << 8,Y ; 5
|
||||
|
||||
LDA WDATA ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
STA zpdummy ; 3
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_15")) ; 3 + 15
|
||||
@ -933,7 +933,7 @@ tickident page, 8
|
||||
.macro op_tick_50 page ; pattern repeats from op_tick_32
|
||||
;4+(4+4+5+4+5+4+5+4+5+4+2+4)+3+16
|
||||
.ident (.concat ("op_tick_50_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -946,7 +946,7 @@ tickident page, 8
|
||||
|
||||
LDA WDATA ; 4
|
||||
NOP ; 2
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_16"))
|
||||
.endmacro
|
||||
@ -954,7 +954,7 @@ tickident page, 8
|
||||
.macro op_tick_52 page ; pattern repeats from op_tick_34
|
||||
;4+(4+4+5+4+5+4+5+4+5+4+4+4)+2+3+12
|
||||
.ident (.concat ("op_tick_52_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -967,7 +967,7 @@ tickident page, 8
|
||||
|
||||
LDA WDATA ; 4
|
||||
STA .ident (.concat ("_op_tick_6_page_", .string(page), "_jmp"))+2 ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
NOP ; 2
|
||||
|
||||
JMP .ident(.concat("_op_tick_page_", .string(page), "_tail_12"))
|
||||
@ -976,7 +976,7 @@ tickident page, 8
|
||||
.macro op_tick_54 page ; pattern repeats from op_tick_36
|
||||
; 4 + (4+4+5+4+5+4+5+3+3+4+5+4+4)+4+4+4+3
|
||||
.ident (.concat ("op_tick_54_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -992,7 +992,7 @@ tickident page, 8
|
||||
STA zpdummy ; 3
|
||||
STA zpdummy ; 3
|
||||
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
; used >3 pad cycles between tick pair; can't branch to tail
|
||||
STA @D+2 ; 4
|
||||
@ -1005,7 +1005,7 @@ tickident page, 8
|
||||
.macro op_tick_56 page
|
||||
; 4+(4+4+5+4+5+4+5+4+5+4+4+4+4)+2+4+4+3
|
||||
.ident (.concat ("op_tick_56_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -1020,7 +1020,7 @@ tickident page, 8
|
||||
STA @D+2 ; 4
|
||||
|
||||
STA dummy ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
; used >3 pad cycles between tick pair; can't branch to tail
|
||||
NOP ; 2
|
||||
@ -1034,7 +1034,7 @@ tickident page, 8
|
||||
.macro op_tick_58 page ; pattern repeats from op_tick_40
|
||||
;4+(4+4+5+4+5+4+5+4+5+4+4+3+3+4)+4+4+3
|
||||
.ident (.concat ("op_tick_58_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -1050,7 +1050,7 @@ tickident page, 8
|
||||
|
||||
STA zpdummy ; 3
|
||||
STA zpdummy ; 3
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
; used >3 pad cycles between tick pair; can't branch to tail
|
||||
LDA WDATA ; 4
|
||||
@ -1062,7 +1062,7 @@ tickident page, 8
|
||||
.macro op_tick_60 page
|
||||
; 4+(4+4+5+4+5+4+5+4+5+4+4+4+4+4)+2+4+3
|
||||
.ident (.concat ("op_tick_60_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -1079,7 +1079,7 @@ tickident page, 8
|
||||
LDA WDATA ; 4
|
||||
|
||||
STA dummy ; 4
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
; used >3 pad cycles between tick pair; can't branch to tail
|
||||
NOP ; 2
|
||||
@ -1091,7 +1091,7 @@ tickident page, 8
|
||||
.macro op_tick_62 page
|
||||
;4+(4+4+5+4+5+4+5+4+5+4+4+4+3+3+4)+4+3
|
||||
.ident (.concat ("op_tick_62_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -1108,7 +1108,7 @@ tickident page, 8
|
||||
|
||||
STA zpdummy ; 3
|
||||
STA zpdummy ; 3
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
; used >3 pad cycles between tick pair; can't branch to tail
|
||||
STA @D+1 ; 4
|
||||
@ -1119,7 +1119,7 @@ tickident page, 8
|
||||
.macro op_tick_64 page
|
||||
;4+(4+4+5+4+5+4+5+4+5+4+4+4+4+4+4)+2+3
|
||||
.ident (.concat ("op_tick_64_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -1137,7 +1137,7 @@ tickident page, 8
|
||||
STA @D+1 ; 4
|
||||
STA dummy ; 4
|
||||
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
NOP ; 2
|
||||
|
||||
@D:
|
||||
@ -1147,7 +1147,7 @@ tickident page, 8
|
||||
.macro op_tick_66 page ; pattern repeats from op_tick_8
|
||||
; 4+(4+4+5+4+5+4+5+4+5+4+4+4+3+4+3+4)+3
|
||||
.ident (.concat ("op_tick_66_page_", .string(page))):
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
LDA WDATA ; 4
|
||||
LDY WDATA ; 4
|
||||
STA page << 8,Y ; 5
|
||||
@ -1166,7 +1166,7 @@ tickident page, 8
|
||||
|
||||
STA zpdummy ; 3
|
||||
STA zpdummy ; 3
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
@D:
|
||||
JMP op_nop ; 3
|
||||
@ -1293,7 +1293,7 @@ op_terminate:
|
||||
; the last 4 bytes in a 2K "TCP frame". i.e. we can assume that we need to consume
|
||||
; exactly 2K from the W5100 socket buffer.
|
||||
op_ack:
|
||||
BIT TICK ; 4
|
||||
STA TICK ; 4
|
||||
|
||||
; allow flip-flopping the PAGE1/PAGE2 soft switches to steer writes to MAIN/AUX screens
|
||||
; actually this allows touching any $C0XX soft-switch, in case that is useful somehow
|
||||
@ -1314,7 +1314,7 @@ op_ack:
|
||||
LDX #<S0RXRD ; 2
|
||||
STX WADRL ; 4
|
||||
|
||||
BIT TICK ; 4 (36)
|
||||
STA TICK ; 4 (36)
|
||||
|
||||
LDA WDATA ; 4 Read high byte
|
||||
; No need to read low byte since it's guaranteed to be 0 since we're at the end of a 2K frame.
|
||||
@ -1339,7 +1339,7 @@ op_ack:
|
||||
; - used as the low byte for resetting the W5100 address pointer when we're ready to start processing more data
|
||||
LDX #$00 ; 2 restore invariant for dispatch loop
|
||||
|
||||
JMP checkrecv ; 3 (37 with following BIT TICK)
|
||||
JMP checkrecv ; 3 (37 with following STA TICK)
|
||||
|
||||
; Quit to ProDOS
|
||||
exit:
|
||||
|
@ -1,25 +1,149 @@
|
||||
"""Apple II logical display colours."""
|
||||
"""Apple II nominal display colours, represented by 4-bit dot sequences.
|
||||
|
||||
These are the "asymptotic" colours as displayed in e.g. continuous runs of
|
||||
pixels. The effective colours that are actually displayed are not discrete,
|
||||
due to NTSC artifacting being a continuous process.
|
||||
"""
|
||||
|
||||
from typing import Tuple, Type
|
||||
|
||||
import enum
|
||||
import functools
|
||||
|
||||
|
||||
class DHGRColours(enum.Enum):
|
||||
# Value is memory bit order, which is opposite to screen order (bits
|
||||
# ordered Left to Right on screen)
|
||||
class NominalColours(enum.Enum):
|
||||
pass
|
||||
|
||||
|
||||
class HGRColours(NominalColours):
|
||||
"""Map from 4-bit dot representation to DHGR pixel colours.
|
||||
|
||||
Dots are in memory bit order (MSB -> LSB), which is opposite to screen
|
||||
order (LSB -> MSB is ordered left-to-right on the screen)
|
||||
|
||||
Note that these are right-rotated from the HGR mapping, because of a
|
||||
1-tick phase difference in the colour reference signal for DHGR vs HGR
|
||||
"""
|
||||
BLACK = 0b0000
|
||||
MAGENTA = 0b0001
|
||||
BROWN = 0b1000
|
||||
ORANGE = 0b1001 # HGR colour
|
||||
DARK_GREEN = 0b0100
|
||||
GREY1 = 0b0101
|
||||
GREEN = 0b1100 # HGR colour
|
||||
YELLOW = 0b1101
|
||||
DARK_BLUE = 0b0010
|
||||
VIOLET = 0b0011 # HGR colour
|
||||
GREY2 = 0b1010
|
||||
PINK = 0b1011
|
||||
MED_BLUE = 0b0110 # HGR colour
|
||||
LIGHT_BLUE = 0b0111
|
||||
AQUA = 0b1110
|
||||
WHITE = 0b1111
|
||||
|
||||
|
||||
class DHGRColours(NominalColours):
|
||||
"""Map from 4-bit dot representation to DHGR pixel colours.
|
||||
|
||||
Dots are in memory bit order (MSB -> LSB), which is opposite to screen
|
||||
order (LSB -> MSB is ordered left-to-right on the screen)
|
||||
|
||||
Note that these are right-rotated from the HGR mapping, because of a
|
||||
1-tick phase difference in the colour reference signal for DHGR vs HGR
|
||||
"""
|
||||
|
||||
# representation.
|
||||
BLACK = 0b0000
|
||||
MAGENTA = 0b1000
|
||||
BROWN = 0b0100
|
||||
ORANGE = 0b1100
|
||||
ORANGE = 0b1100 # HGR colour
|
||||
DARK_GREEN = 0b0010
|
||||
GREY1 = 0b1010
|
||||
GREEN = 0b0110
|
||||
GREEN = 0b0110 # HGR colour
|
||||
YELLOW = 0b1110
|
||||
DARK_BLUE = 0b0001
|
||||
VIOLET = 0b1001
|
||||
VIOLET = 0b1001 # HGR colour
|
||||
GREY2 = 0b0101
|
||||
PINK = 0b1101
|
||||
MED_BLUE = 0b0011
|
||||
MED_BLUE = 0b0011 # HGR colour
|
||||
LIGHT_BLUE = 0b1011
|
||||
AQUA = 0b0111
|
||||
WHITE = 0b1111
|
||||
|
||||
|
||||
def ror(int4: int, howmany: int) -> int:
|
||||
"""Rotate-right an int4 some number of times."""
|
||||
res = int4
|
||||
for _ in range(howmany):
|
||||
res = _ror(res)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def _ror(int4: int) -> int:
|
||||
return ((int4 & 0b1110) >> 1) ^ ((int4 & 0b0001) << 3)
|
||||
|
||||
|
||||
def rol(int4: int, howmany: int) -> int:
|
||||
"""Rotate-left an int4 some number of times."""
|
||||
res = int4
|
||||
for _ in range(howmany):
|
||||
res = _rol(res)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def _rol(int4: int) -> int:
|
||||
return ((int4 & 0b0111) << 1) ^ ((int4 & 0b1000) >> 3)
|
||||
|
||||
|
||||
@functools.lru_cache(None)
|
||||
def dots_to_nominal_colour_pixels(
|
||||
num_bits: int,
|
||||
dots: int,
|
||||
colours: Type[NominalColours],
|
||||
init_phase: int = 1 # Such that phase = 0 at start of body
|
||||
) -> Tuple[NominalColours]:
|
||||
"""Sequence of num_bits nominal colour pixels via sliding 4-bit window.
|
||||
|
||||
Includes the 3-bit header that represents the trailing 3 bits of the
|
||||
previous tuple body. e.g. for DHGR, storing a byte in aux even columns
|
||||
will also influence the colours of the previous main odd column.
|
||||
|
||||
This naively models (approximates) the NTSC colour artifacting.
|
||||
|
||||
TODO: Use a more careful analogue colour composition model to produce
|
||||
effective pixel colours.
|
||||
|
||||
TODO: DHGR vs HGR colour differences can be modeled by changing init_phase
|
||||
"""
|
||||
res = []
|
||||
|
||||
shifted = dots
|
||||
phase = init_phase
|
||||
|
||||
for i in range(num_bits):
|
||||
colour = rol(shifted & 0b1111, phase)
|
||||
res.append(colours(colour))
|
||||
|
||||
shifted >>= 1
|
||||
phase += 1
|
||||
if phase == 4:
|
||||
phase = 0
|
||||
|
||||
return tuple(res)
|
||||
|
||||
|
||||
@functools.lru_cache(None)
|
||||
def dots_to_nominal_colour_pixel_values(
|
||||
num_bits: int,
|
||||
dots: int,
|
||||
colours: Type[NominalColours],
|
||||
init_phase: int = 1 # Such that phase = 0 at start of body
|
||||
) -> Tuple[int]:
|
||||
""""Sequence of num_bits nominal colour values via sliding 4-bit window."""
|
||||
|
||||
return tuple(p.value for p in dots_to_nominal_colour_pixels(
|
||||
num_bits, dots, colours, init_phase
|
||||
))
|
||||
|
||||
|
113
transcoder/colours_test.py
Normal file
113
transcoder/colours_test.py
Normal file
@ -0,0 +1,113 @@
|
||||
import unittest
|
||||
|
||||
import colours
|
||||
|
||||
HGRColours = colours.HGRColours
|
||||
|
||||
|
||||
class TestColours(unittest.TestCase):
|
||||
|
||||
def test_dots_to_pixels(self):
|
||||
self.assertEqual(
|
||||
(
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.DARK_BLUE,
|
||||
HGRColours.MED_BLUE,
|
||||
HGRColours.AQUA,
|
||||
HGRColours.AQUA,
|
||||
HGRColours.GREEN,
|
||||
HGRColours.BROWN,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK
|
||||
),
|
||||
colours.dots_to_nominal_colour_pixels(
|
||||
31, 0b00000000000000000000111000000000, HGRColours, init_phase=0
|
||||
)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
(
|
||||
HGRColours.BLACK,
|
||||
HGRColours.MAGENTA,
|
||||
HGRColours.VIOLET,
|
||||
HGRColours.LIGHT_BLUE,
|
||||
HGRColours.WHITE,
|
||||
HGRColours.AQUA,
|
||||
HGRColours.GREEN,
|
||||
HGRColours.BROWN,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.MAGENTA,
|
||||
HGRColours.VIOLET,
|
||||
HGRColours.LIGHT_BLUE,
|
||||
HGRColours.WHITE,
|
||||
HGRColours.AQUA,
|
||||
HGRColours.GREEN,
|
||||
HGRColours.BROWN,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.MAGENTA,
|
||||
HGRColours.VIOLET,
|
||||
HGRColours.LIGHT_BLUE,
|
||||
HGRColours.WHITE,
|
||||
HGRColours.AQUA,
|
||||
HGRColours.GREEN,
|
||||
HGRColours.BROWN,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK,
|
||||
HGRColours.BLACK
|
||||
),
|
||||
colours.dots_to_nominal_colour_pixels(
|
||||
31, 0b0000111100001111000011110000, HGRColours, init_phase=0
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class TestRolRoR(unittest.TestCase):
|
||||
def testRolOne(self):
|
||||
self.assertEqual(0b1111, colours.rol(0b1111, 1))
|
||||
self.assertEqual(0b0001, colours.rol(0b1000, 1))
|
||||
self.assertEqual(0b1010, colours.rol(0b0101, 1))
|
||||
|
||||
def testRolMany(self):
|
||||
self.assertEqual(0b1111, colours.rol(0b1111, 3))
|
||||
self.assertEqual(0b0010, colours.rol(0b1000, 2))
|
||||
self.assertEqual(0b0101, colours.rol(0b0101, 2))
|
||||
|
||||
def testRorOne(self):
|
||||
self.assertEqual(0b1111, colours.ror(0b1111, 1))
|
||||
self.assertEqual(0b1000, colours.ror(0b0001, 1))
|
||||
self.assertEqual(0b0101, colours.ror(0b1010, 1))
|
||||
|
||||
def testRoRMany(self):
|
||||
self.assertEqual(0b1111, colours.ror(0b1111, 3))
|
||||
self.assertEqual(0b1000, colours.ror(0b0010, 2))
|
||||
self.assertEqual(0b0101, colours.ror(0b0101, 2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
1
transcoder/data/.gitattributes
vendored
Normal file
1
transcoder/data/.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
3
transcoder/data/DHGR_palette_0_edit_distance.pickle.bz2
Normal file
3
transcoder/data/DHGR_palette_0_edit_distance.pickle.bz2
Normal file
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:b47eadfdf8c8e16c6539f9a16ed0b5a393b17e0cbd03831aacda7f659e9522d6
|
||||
size 120830327
|
3
transcoder/data/DHGR_palette_5_edit_distance.pickle.bz2
Normal file
3
transcoder/data/DHGR_palette_5_edit_distance.pickle.bz2
Normal file
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:8c245981f91ffa89b47abdd1c9d646c2e79499a0c82c38c91234be0a59e52f1f
|
||||
size 118832545
|
3
transcoder/data/HGR_palette_0_edit_distance.pickle.bz2
Normal file
3
transcoder/data/HGR_palette_0_edit_distance.pickle.bz2
Normal file
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:3fd52feb08eb6f99b267a1050c68905f25d0d106ad7c2c63473cc0a0f6aa1b25
|
||||
size 224334626
|
3
transcoder/data/HGR_palette_5_edit_distance.pickle.bz2
Normal file
3
transcoder/data/HGR_palette_5_edit_distance.pickle.bz2
Normal file
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:dbf83e3d0b6c7867ccf7ae1d55a6ed4e906409b08043dec514e1104cec95f0fc
|
||||
size 220565577
|
Binary file not shown.
Binary file not shown.
@ -54,7 +54,7 @@ class FileFrameGrabber(FrameGrabber):
|
||||
return "P%d" % self.palette.value
|
||||
|
||||
def frames(self) -> Iterator[screen.MemoryMap]:
|
||||
"""Encode frame to HGR using bmp2dhr.
|
||||
"""Encode frame to (D)HGR using bmp2dhr.
|
||||
|
||||
We do the encoding in a background thread to parallelize.
|
||||
"""
|
||||
|
@ -3,12 +3,11 @@
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
# TODO: screen memory changes should happen via Machine while emitting opcodes?
|
||||
|
||||
class Machine:
|
||||
"""Represents Apple II and player virtual machine state."""
|
||||
|
||||
def __init__(self):
|
||||
self.page = 0x20 # type: int
|
||||
|
||||
def emit(self, opcode: "Opcode") -> Iterator[int]:
|
||||
"""
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
"""Transcodes an input video file to ][Vision format."""
|
||||
"""Transcodes an input video file to ][-Vision format."""
|
||||
|
||||
import argparse
|
||||
|
||||
@ -7,7 +7,7 @@ import palette
|
||||
import video_mode
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Transcode videos to ][Vision format.')
|
||||
description='Transcode videos to ][-Vision format.')
|
||||
parser.add_argument(
|
||||
'input', help='Path to input video file.')
|
||||
parser.add_argument(
|
||||
|
@ -1,6 +1,7 @@
|
||||
import bz2
|
||||
import functools
|
||||
import pickle
|
||||
import sys
|
||||
from typing import Iterable, Type
|
||||
|
||||
import colormath.color_conversions
|
||||
@ -8,66 +9,12 @@ import colormath.color_diff
|
||||
import colormath.color_objects
|
||||
import numpy as np
|
||||
import weighted_levenshtein
|
||||
from etaprogress.progress import ProgressBar
|
||||
|
||||
import colours
|
||||
import palette
|
||||
import screen
|
||||
|
||||
# The DHGR display encodes 7 pixels across interleaved 4-byte sequences
|
||||
# of AUX and MAIN memory, as follows:
|
||||
#
|
||||
# PBBBAAAA PDDCCCCB PFEEEEDD PGGGGFFF
|
||||
# Aux N Main N Aux N+1 Main N+1 (N even)
|
||||
#
|
||||
# Where A..G are the pixels, and P represents the (unused) palette bit.
|
||||
#
|
||||
# This layout makes more sense when written as a (little-endian) 32-bit integer:
|
||||
#
|
||||
# 33222222222211111111110000000000 <- bit pos in uint32
|
||||
# 10987654321098765432109876543210
|
||||
# PGGGGFFFPFEEEEDDPDDCCCCBPBBBAAAA
|
||||
#
|
||||
# i.e. apart from the palette bits this is a linear ordering of pixels,
|
||||
# when read from LSB to MSB (i.e. right-to-left). i.e. the screen layout order
|
||||
# of bits is opposite to the usual binary representation ordering.
|
||||
#
|
||||
# If we now look at the effect of storing a byte in each of the 4
|
||||
# byte-offset positions within this uint32,
|
||||
#
|
||||
# PGGGGFFFPFEEEEDDPDDCCCCBPBBBAAAA
|
||||
# 33333333222222221111111100000000
|
||||
#
|
||||
# We see that these byte offsets cause changes to the following pixels:
|
||||
#
|
||||
# 0: A B
|
||||
# 1: B C D
|
||||
# 2: D E F
|
||||
# 3: F G
|
||||
#
|
||||
# i.e. DHGR byte stores to offsets 0 and 3 result in changing one 8-bit value
|
||||
# (2 DHGR pixels) into another; offsets 1 and 3 result in changing one 12-bit
|
||||
# value (3 DHGR pixels).
|
||||
#
|
||||
# We can simplify things by stripping out the palette bit and packing
|
||||
# down to a 28-bit integer representation:
|
||||
#
|
||||
# 33222222222211111111110000000000 <- bit pos in uint32
|
||||
# 10987654321098765432109876543210
|
||||
#
|
||||
# 0000GGGGFFFFEEEEDDDDCCCCBBBBAAAA <- pixel A..G
|
||||
# 3210321032103210321032103210 <- bit pos in A..G pixel
|
||||
#
|
||||
# 3333333222222211111110000000 <- byte offset 0.3
|
||||
#
|
||||
# With this representation, we can precompute an edit distance for the
|
||||
# pixel changes resulting from all possible DHGR byte stores.
|
||||
#
|
||||
# We further encode these (source, target) -> distance mappings by
|
||||
# concatenating source and target into 16- or 24-bit values. This is
|
||||
# efficient to work with in the video transcoder.
|
||||
#
|
||||
# Since we are enumerating all such 16- or 24-bit values, these can be packed
|
||||
# contiguously into an array whose index is the (source, target) pair and
|
||||
# the value is the edit distance.
|
||||
|
||||
PIXEL_CHARS = "0123456789ABCDEF"
|
||||
|
||||
@ -77,57 +24,13 @@ def pixel_char(i: int) -> str:
|
||||
|
||||
|
||||
@functools.lru_cache(None)
|
||||
def pixel_string(pixels: Iterable[colours.DHGRColours]) -> str:
|
||||
return "".join(pixel_char(p.value) for p in pixels)
|
||||
|
||||
|
||||
@functools.lru_cache(None)
|
||||
def pixels_influenced_by_byte_index(
|
||||
pixels: str,
|
||||
idx: int) -> str:
|
||||
"""Return subset of pixels that are influenced by given byte index (0..4)"""
|
||||
start, end = {
|
||||
0: (0, 1),
|
||||
1: (1, 3),
|
||||
2: (3, 5),
|
||||
3: (5, 6)
|
||||
}[idx]
|
||||
|
||||
return pixels[start:end + 1]
|
||||
|
||||
|
||||
@functools.lru_cache(None)
|
||||
def int28_to_pixels(int28):
|
||||
return tuple(
|
||||
palette.DHGRColours(
|
||||
(int28 & (0b1111 << (4 * i))) >> (4 * i)) for i in range(7)
|
||||
)
|
||||
|
||||
|
||||
# TODO: these duplicates byte_mask32/byte_shift from DHGRBitmap
|
||||
|
||||
# Map n-bit int into 32-bit masked value
|
||||
def map_int8_to_mask32_0(int8):
|
||||
assert 0 <= int8 < 2 ** 8, int8
|
||||
return int8
|
||||
|
||||
|
||||
def map_int12_to_mask32_1(int12):
|
||||
assert 0 <= int12 < 2 ** 12, int12
|
||||
return int12 << 4
|
||||
|
||||
|
||||
def map_int12_to_mask32_2(int12):
|
||||
assert 0 <= int12 < 2 ** 12, int12
|
||||
return int12 << 12
|
||||
|
||||
|
||||
def map_int8_to_mask32_3(int8):
|
||||
assert 0 <= int8 < 2 ** 8, int8
|
||||
return int8 << 20
|
||||
def pixel_string(pixels: Iterable[int]) -> str:
|
||||
return "".join(pixel_char(p) for p in pixels)
|
||||
|
||||
|
||||
class EditDistanceParams:
|
||||
"""Data class for parameters to Damerau-Levenshtein edit distance."""
|
||||
|
||||
# Don't even consider insertions and deletions into the string, they don't
|
||||
# make sense for comparing pixel strings
|
||||
insert_costs = np.ones(128, dtype=np.float64) * 100000
|
||||
@ -135,20 +38,26 @@ class EditDistanceParams:
|
||||
|
||||
# Smallest substitution value is ~20 from palette.diff_matrices, i.e.
|
||||
# we always prefer to transpose 2 pixels rather than substituting colours.
|
||||
transpose_costs = np.ones((128, 128), dtype=np.float64) * 10
|
||||
# TODO: is quality really better allowing transposes?
|
||||
transpose_costs = np.ones((128, 128), dtype=np.float64) * 100000 # 10
|
||||
|
||||
# These will be filled in later
|
||||
substitute_costs = np.zeros((128, 128), dtype=np.float64)
|
||||
|
||||
# Substitution costs to use when evaluating other potential offsets at which
|
||||
# to store a content byte. We penalize more harshly for introducing
|
||||
# errors that alter pixel colours, since these tend to be very
|
||||
# noticeable as visual noise.
|
||||
#
|
||||
# TODO: currently unused
|
||||
error_substitute_costs = np.zeros((128, 128), dtype=np.float64)
|
||||
|
||||
|
||||
def compute_diff_matrix(pal: Type[palette.BasePalette]):
|
||||
# Compute matrix of CIE2000 delta values for this pal, representing
|
||||
# perceptual distance between colours.
|
||||
"""Compute matrix of perceptual distance between colour pairs.
|
||||
|
||||
Specifically CIE2000 delta values for this palette.
|
||||
"""
|
||||
dm = np.ndarray(shape=(16, 16), dtype=np.int)
|
||||
|
||||
for colour1, a in pal.RGB.items():
|
||||
@ -162,7 +71,9 @@ def compute_diff_matrix(pal: Type[palette.BasePalette]):
|
||||
return dm
|
||||
|
||||
|
||||
def make_substitute_costs(pal: Type[palette.BasePalette]):
|
||||
def compute_substitute_costs(pal: Type[palette.BasePalette]):
|
||||
"""Compute costs for substituting one colour pixel for another."""
|
||||
|
||||
edp = EditDistanceParams()
|
||||
|
||||
diff_matrix = compute_diff_matrix(pal)
|
||||
@ -171,20 +82,20 @@ def make_substitute_costs(pal: Type[palette.BasePalette]):
|
||||
for i, c in enumerate(PIXEL_CHARS):
|
||||
for j, d in enumerate(PIXEL_CHARS):
|
||||
cost = diff_matrix[i, j]
|
||||
edp.substitute_costs[(ord(c), ord(d))] = cost # / 20
|
||||
edp.substitute_costs[(ord(d), ord(c))] = cost # / 20
|
||||
edp.error_substitute_costs[(ord(c), ord(d))] = 5 * cost # / 4
|
||||
edp.error_substitute_costs[(ord(d), ord(c))] = 5 * cost # / 4
|
||||
edp.substitute_costs[(ord(c), ord(d))] = cost
|
||||
edp.substitute_costs[(ord(d), ord(c))] = cost
|
||||
edp.error_substitute_costs[(ord(c), ord(d))] = 5 * cost
|
||||
edp.error_substitute_costs[(ord(d), ord(c))] = 5 * cost
|
||||
|
||||
return edp
|
||||
|
||||
|
||||
@functools.lru_cache(None)
|
||||
def edit_distance(
|
||||
edp: EditDistanceParams,
|
||||
a: str,
|
||||
b: str,
|
||||
error: bool) -> np.float64:
|
||||
"""Damerau-Levenshtein edit distance between two pixel strings."""
|
||||
res = weighted_levenshtein.dam_lev(
|
||||
a, b,
|
||||
insert_costs=edp.insert_costs,
|
||||
@ -193,86 +104,100 @@ def edit_distance(
|
||||
edp.error_substitute_costs if error else edp.substitute_costs),
|
||||
)
|
||||
|
||||
assert res == 0 or (1 <= res < 2 ** 16), res
|
||||
# Make sure result can fit in a uint16
|
||||
assert (0 <= res < 2 ** 16), res
|
||||
return res
|
||||
|
||||
|
||||
def make_edit_distance(edp: EditDistanceParams):
|
||||
edit = [
|
||||
np.zeros(shape=(2 ** 16), dtype=np.int16),
|
||||
np.zeros(shape=(2 ** 24), dtype=np.int16),
|
||||
np.zeros(shape=(2 ** 24), dtype=np.int16),
|
||||
np.zeros(shape=(2 ** 16), dtype=np.int16),
|
||||
]
|
||||
def compute_edit_distance(
|
||||
edp: EditDistanceParams,
|
||||
bitmap_cls: Type[screen.Bitmap],
|
||||
nominal_colours: Type[colours.NominalColours]
|
||||
):
|
||||
"""Computes edit distance matrix between all pairs of pixel strings.
|
||||
|
||||
for i in range(2 ** 8):
|
||||
print(i)
|
||||
for j in range(2 ** 8):
|
||||
pair = (i << 8) + j
|
||||
Enumerates all possible values of the masked bit representation from
|
||||
bitmap_cls (assuming it is contiguous, i.e. we enumerate all
|
||||
2**bitmap_cls.MASKED_BITS values). These are mapped to the dot
|
||||
representation, turned into coloured pixel strings, and we compute the
|
||||
edit distance.
|
||||
|
||||
first = map_int8_to_mask32_0(i)
|
||||
second = map_int8_to_mask32_0(j)
|
||||
The effect of this is that we precompute the effect of storing all possible
|
||||
byte values against all possible screen backgrounds (e.g. as
|
||||
influencing/influenced by neighbouring bytes).
|
||||
"""
|
||||
|
||||
first_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(first)), 0)
|
||||
second_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(second)), 0)
|
||||
bits = bitmap_cls.MASKED_BITS
|
||||
|
||||
edit[0][pair] = edit_distance(
|
||||
edp, first_pixels, second_pixels, error=False)
|
||||
bitrange = np.uint64(2 ** bits)
|
||||
|
||||
first = map_int8_to_mask32_3(i)
|
||||
second = map_int8_to_mask32_3(j)
|
||||
edit = []
|
||||
for _ in range(len(bitmap_cls.BYTE_MASKS)):
|
||||
edit.append(
|
||||
np.zeros(shape=np.uint64(bitrange * bitrange), dtype=np.uint16))
|
||||
|
||||
first_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(first)), 3)
|
||||
second_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(second)), 3)
|
||||
# Matrix is symmetrical with zero diagonal so only need to compute upper
|
||||
# triangle
|
||||
bar = ProgressBar((bitrange * (bitrange - 1)) / 2, max_width=80)
|
||||
|
||||
edit[3][pair] = edit_distance(
|
||||
edp, first_pixels, second_pixels, error=False)
|
||||
num_dots = bitmap_cls.MASKED_DOTS
|
||||
|
||||
for i in range(2 ** 12):
|
||||
print(i)
|
||||
for j in range(2 ** 12):
|
||||
pair = (i << 12) + j
|
||||
cnt = 0
|
||||
for i in range(np.uint64(bitrange)):
|
||||
for j in range(i):
|
||||
cnt += 1
|
||||
|
||||
first = map_int12_to_mask32_1(i)
|
||||
second = map_int12_to_mask32_1(j)
|
||||
if cnt % 10000 == 0:
|
||||
bar.numerator = cnt
|
||||
print(bar, end='\r')
|
||||
sys.stdout.flush()
|
||||
|
||||
first_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(first)), 1)
|
||||
second_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(second)), 1)
|
||||
pair = (np.uint64(i) << bits) + np.uint64(j)
|
||||
|
||||
edit[1][pair] = edit_distance(
|
||||
edp, first_pixels, second_pixels, error=False)
|
||||
for o, ph in enumerate(bitmap_cls.PHASES):
|
||||
first_dots = bitmap_cls.to_dots(i, byte_offset=o)
|
||||
second_dots = bitmap_cls.to_dots(j, byte_offset=o)
|
||||
|
||||
first = map_int12_to_mask32_2(i)
|
||||
second = map_int12_to_mask32_2(j)
|
||||
|
||||
first_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(first)), 2)
|
||||
second_pixels = pixels_influenced_by_byte_index(
|
||||
pixel_string(int28_to_pixels(second)), 2)
|
||||
|
||||
edit[2][pair] = edit_distance(
|
||||
edp, first_pixels, second_pixels, error=False)
|
||||
first_pixels = pixel_string(
|
||||
colours.dots_to_nominal_colour_pixel_values(
|
||||
num_dots, first_dots, nominal_colours,
|
||||
init_phase=ph)
|
||||
)
|
||||
second_pixels = pixel_string(
|
||||
colours.dots_to_nominal_colour_pixel_values(
|
||||
num_dots, second_dots, nominal_colours,
|
||||
init_phase=ph)
|
||||
)
|
||||
edit[o][pair] = edit_distance(
|
||||
edp, first_pixels, second_pixels, error=False)
|
||||
|
||||
return edit
|
||||
|
||||
|
||||
def make_edit_distance(
|
||||
pal: Type[palette.BasePalette],
|
||||
edp: EditDistanceParams,
|
||||
bitmap_cls: Type[screen.Bitmap],
|
||||
nominal_colours: Type[colours.NominalColours]
|
||||
):
|
||||
"""Write file containing (D)HGR edit distance matrix for a palette."""
|
||||
|
||||
dist = compute_edit_distance(edp, bitmap_cls, nominal_colours)
|
||||
data = "transcoder/data/%s_palette_%d_edit_distance.pickle.bz2" % (
|
||||
bitmap_cls.NAME, pal.ID.value)
|
||||
with bz2.open(data, "wb", compresslevel=9) as out:
|
||||
pickle.dump(dist, out, protocol=pickle.HIGHEST_PROTOCOL)
|
||||
|
||||
|
||||
def main():
|
||||
for p in palette.PALETTES.values():
|
||||
print("Processing palette %s" % p)
|
||||
edp = make_substitute_costs(p)
|
||||
edit = make_edit_distance(edp)
|
||||
edp = compute_substitute_costs(p)
|
||||
|
||||
# TODO: error distance matrices
|
||||
data = "transcoder/data/palette_%d_edit_distance.pickle" \
|
||||
".bz2" % p.ID.value
|
||||
with bz2.open(data, "wb", compresslevel=9) as out:
|
||||
pickle.dump(edit, out, protocol=pickle.HIGHEST_PROTOCOL)
|
||||
# TODO: still worth using error distance matrices?
|
||||
|
||||
make_edit_distance(p, edp, screen.HGRBitmap, colours.HGRColours)
|
||||
make_edit_distance(p, edp, screen.DHGRBitmap, colours.DHGRColours)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -1,82 +1,98 @@
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from colours import DHGRColours
|
||||
import numpy as np
|
||||
from etaprogress.progress import ProgressBar
|
||||
|
||||
import make_data_tables
|
||||
import screen
|
||||
from colours import HGRColours
|
||||
from palette import PALETTES
|
||||
|
||||
|
||||
class TestMakeDataTables(unittest.TestCase):
|
||||
def test_pixel_string(self):
|
||||
pixels = (DHGRColours.BLACK, DHGRColours.WHITE, DHGRColours.ORANGE)
|
||||
pixels = (HGRColours.BLACK, HGRColours.WHITE, HGRColours.ORANGE)
|
||||
self.assertEqual("0FC", make_data_tables.pixel_string(pixels))
|
||||
|
||||
def test_pixels_influenced_by_byte_index(self):
|
||||
pixels = "CB00000"
|
||||
self.assertEqual(
|
||||
"CB",
|
||||
make_data_tables.pixels_influenced_by_byte_index(pixels, 0)
|
||||
)
|
||||
def test_edit_distances_dhgr(self):
|
||||
"""Assert invariants and symmetries of the edit distance matrices."""
|
||||
for p in PALETTES:
|
||||
ed = screen.DHGRBitmap.edit_distances(p)
|
||||
print(p)
|
||||
|
||||
pixels = "CBA9000"
|
||||
self.assertEqual(
|
||||
"BA9",
|
||||
make_data_tables.pixels_influenced_by_byte_index(pixels, 1)
|
||||
)
|
||||
bar = ProgressBar((4 * 2 ** 13 * (2 ** 13 - 1)) / 2, max_width=80)
|
||||
|
||||
def test_int28_to_pixels(self):
|
||||
self.assertEqual(
|
||||
(
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.YELLOW,
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.BLACK,
|
||||
),
|
||||
tuple(
|
||||
make_data_tables.int28_to_pixels(
|
||||
0b00000000000000000000111000000000)
|
||||
)
|
||||
)
|
||||
cnt = 0
|
||||
for ph in range(3):
|
||||
|
||||
self.assertEqual(
|
||||
(
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.WHITE,
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.WHITE,
|
||||
DHGRColours.BLACK,
|
||||
DHGRColours.WHITE,
|
||||
DHGRColours.BLACK,
|
||||
),
|
||||
tuple(
|
||||
make_data_tables.int28_to_pixels(
|
||||
0b0000111100001111000011110000)
|
||||
)
|
||||
)
|
||||
# Only zero entries should be on diagonal, i.e. of form
|
||||
# i << 13 + i
|
||||
zeros = np.arange(len(ed[ph]))[ed[ph] == 0]
|
||||
for z in zeros:
|
||||
z1 = z & (2 ** 13 - 1)
|
||||
z2 = (z >> 13) & (2 ** 13 - 1)
|
||||
self.assertEqual(z1, z2)
|
||||
|
||||
def test_map_to_mask32(self):
|
||||
byte_mask32 = [
|
||||
# 33222222222211111111110000000000 <- bit pos in uint32
|
||||
# 10987654321098765432109876543210
|
||||
# 0000GGGGFFFFEEEEDDDDCCCCBBBBAAAA <- pixel A..G
|
||||
# 3210321032103210321032103210 <- bit pos in A..G pixel
|
||||
0b00000000000000000000000011111111, # byte 0 influences A,B
|
||||
0b00000000000000001111111111110000, # byte 1 influences B,C,D
|
||||
0b00000000111111111111000000000000, # byte 2 influences D,E,F
|
||||
0b00001111111100000000000000000000, # byte 3 influences F,G
|
||||
]
|
||||
int8_max = 2 ** 8 - 1
|
||||
int12_max = 2 ** 12 - 1
|
||||
# Assert that matrix is symmetrical
|
||||
for i in range(2 ** 13):
|
||||
for j in range(i):
|
||||
cnt += 1
|
||||
|
||||
self.assertEqual(
|
||||
make_data_tables.map_int8_to_mask32_0(int8_max), byte_mask32[0])
|
||||
self.assertEqual(
|
||||
make_data_tables.map_int12_to_mask32_1(int12_max), byte_mask32[1])
|
||||
self.assertEqual(
|
||||
make_data_tables.map_int12_to_mask32_2(int12_max), byte_mask32[2])
|
||||
self.assertEqual(
|
||||
make_data_tables.map_int8_to_mask32_3(int8_max), byte_mask32[3])
|
||||
if cnt % 10000 == 0:
|
||||
bar.numerator = cnt
|
||||
print(bar, end='\r')
|
||||
sys.stdout.flush()
|
||||
|
||||
self.assertEqual(
|
||||
ed[ph][(i << 13) + j],
|
||||
ed[ph][(j << 13) + i],
|
||||
)
|
||||
|
||||
# Matrix is positive definite
|
||||
self.assertGreaterEqual(ed[ph][(i << 13) + j], 0)
|
||||
|
||||
def test_edit_distances_hgr(self):
|
||||
"""Assert invariants and symmetries of the edit distance matrices."""
|
||||
|
||||
for p in PALETTES:
|
||||
ed = screen.HGRBitmap.edit_distances(p)
|
||||
print(p)
|
||||
|
||||
bar = ProgressBar((4 * 2 ** 14 * (2 ** 14 - 1)) / 2, max_width=80)
|
||||
|
||||
cnt = 0
|
||||
for ph in range(2):
|
||||
|
||||
# TODO: for HGR this invariant isn't true, all-0 and all-1
|
||||
# values for header/footer/body with/without palette bit can
|
||||
# also have zero difference
|
||||
# # Only zero entries should be on diagonal, i.e. of form
|
||||
# # i << 14 + i
|
||||
# zeros = np.arange(len(ed[ph]))[ed[ph] == 0]
|
||||
# for z in zeros:
|
||||
# z1 = z & (2**14-1)
|
||||
# z2 = (z >> 14) & (2**14-1)
|
||||
# if z1 != z2:
|
||||
# self.assertEqual(z1, z2)
|
||||
|
||||
# Assert that matrix is symmetrical
|
||||
for i in range(2 ** 14):
|
||||
for j in range(i):
|
||||
cnt += 1
|
||||
|
||||
if cnt % 10000 == 0:
|
||||
bar.numerator = cnt
|
||||
print(bar, end='\r')
|
||||
sys.stdout.flush()
|
||||
|
||||
self.assertEqual(
|
||||
ed[ph][(i << 14) + j],
|
||||
ed[ph][(j << 14) + i],
|
||||
)
|
||||
|
||||
# Matrix is positive definite
|
||||
self.assertGreaterEqual(ed[ph][(i << 14) + j], 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -76,6 +76,7 @@ class Movie:
|
||||
|
||||
if aux:
|
||||
aux_seq = self.video.encode_frame(aux, is_aux=True)
|
||||
|
||||
# au has range -15 .. 16 (step=1)
|
||||
# Tick cycles are units of 2
|
||||
tick = au * 2 # -30 .. 32 (step=2)
|
||||
@ -86,18 +87,20 @@ class Movie:
|
||||
|
||||
yield opcodes.TICK_OPCODES[(tick, page)](content, offsets)
|
||||
|
||||
def _emit_bytes(self, _op):
|
||||
"""
|
||||
def _emit_bytes(self, _op: opcodes.Opcode) -> Iterable[int]:
|
||||
"""Emit compiled bytes corresponding to a player opcode.
|
||||
|
||||
:param _op:
|
||||
:return:
|
||||
Also tracks byte stream position.
|
||||
"""
|
||||
for b in self.state.emit(_op):
|
||||
yield b
|
||||
self.stream_pos += 1
|
||||
|
||||
def emit_stream(self, ops: Iterable[opcodes.Opcode]) -> Iterator[int]:
|
||||
"""
|
||||
"""Emit compiled byte stream corresponding to opcode stream.
|
||||
|
||||
Inserts padding opcodes at 2KB stream boundaries, to instruct player
|
||||
to manage the TCP socket buffer.
|
||||
|
||||
:param ops:
|
||||
:return:
|
||||
@ -124,7 +127,7 @@ class Movie:
|
||||
yield from self.done()
|
||||
|
||||
def done(self) -> Iterator[int]:
|
||||
"""Terminate opcode stream.
|
||||
"""Terminate byte stream by emitting terminal opcode and padding to 2KB.
|
||||
|
||||
:return:
|
||||
"""
|
||||
|
@ -1,9 +1,11 @@
|
||||
"""RGB palette values for rendering NominalColour pixels."""
|
||||
|
||||
import enum
|
||||
from typing import Dict, Type
|
||||
|
||||
import colormath.color_objects
|
||||
|
||||
from colours import DHGRColours
|
||||
from colours import HGRColours
|
||||
|
||||
# Type annotation
|
||||
RGB = colormath.color_objects.sRGBColor
|
||||
@ -14,7 +16,8 @@ def rgb(r, g, b):
|
||||
|
||||
|
||||
class Palette(enum.Enum):
|
||||
"""BMP2DHR palette numbers"""
|
||||
"""BMP2DHR palette numbers."""
|
||||
|
||||
UNKNOWN = -1
|
||||
IIGS = 0
|
||||
NTSC = 5
|
||||
@ -24,7 +27,7 @@ class BasePalette:
|
||||
ID = Palette.UNKNOWN # type: Palette
|
||||
|
||||
# Palette RGB map
|
||||
RGB = {} # type: Dict[DHGRColours: RGB]
|
||||
RGB = {} # type: Dict[HGRColours: RGB]
|
||||
|
||||
|
||||
class NTSCPalette(BasePalette):
|
||||
@ -32,22 +35,22 @@ class NTSCPalette(BasePalette):
|
||||
|
||||
# Palette RGB values taken from BMP2DHGR's default NTSC palette
|
||||
RGB = {
|
||||
DHGRColours.BLACK: rgb(0, 0, 0),
|
||||
DHGRColours.MAGENTA: rgb(148, 12, 125),
|
||||
DHGRColours.BROWN: rgb(99, 77, 0),
|
||||
DHGRColours.ORANGE: rgb(249, 86, 29),
|
||||
DHGRColours.DARK_GREEN: rgb(51, 111, 0),
|
||||
DHGRColours.GREY1: rgb(126, 126, 126),
|
||||
DHGRColours.GREEN: rgb(67, 200, 0),
|
||||
DHGRColours.YELLOW: rgb(221, 206, 23),
|
||||
DHGRColours.DARK_BLUE: rgb(32, 54, 212),
|
||||
DHGRColours.VIOLET: rgb(188, 55, 255),
|
||||
DHGRColours.GREY2: rgb(126, 126, 126),
|
||||
DHGRColours.PINK: rgb(255, 129, 236),
|
||||
DHGRColours.MED_BLUE: rgb(7, 168, 225),
|
||||
DHGRColours.LIGHT_BLUE: rgb(158, 172, 255),
|
||||
DHGRColours.AQUA: rgb(93, 248, 133),
|
||||
DHGRColours.WHITE: rgb(255, 255, 255)
|
||||
HGRColours.BLACK: rgb(0, 0, 0),
|
||||
HGRColours.MAGENTA: rgb(148, 12, 125),
|
||||
HGRColours.BROWN: rgb(99, 77, 0),
|
||||
HGRColours.ORANGE: rgb(249, 86, 29),
|
||||
HGRColours.DARK_GREEN: rgb(51, 111, 0),
|
||||
HGRColours.GREY1: rgb(126, 126, 126),
|
||||
HGRColours.GREEN: rgb(67, 200, 0),
|
||||
HGRColours.YELLOW: rgb(221, 206, 23),
|
||||
HGRColours.DARK_BLUE: rgb(32, 54, 212),
|
||||
HGRColours.VIOLET: rgb(188, 55, 255),
|
||||
HGRColours.GREY2: rgb(126, 126, 126),
|
||||
HGRColours.PINK: rgb(255, 129, 236),
|
||||
HGRColours.MED_BLUE: rgb(7, 168, 225),
|
||||
HGRColours.LIGHT_BLUE: rgb(158, 172, 255),
|
||||
HGRColours.AQUA: rgb(93, 248, 133),
|
||||
HGRColours.WHITE: rgb(255, 255, 255)
|
||||
}
|
||||
|
||||
|
||||
@ -56,22 +59,22 @@ class IIGSPalette(BasePalette):
|
||||
|
||||
# Palette RGB values taken from BMP2DHGR's KEGS32 palette
|
||||
RGB = {
|
||||
DHGRColours.BLACK: rgb(0, 0, 0),
|
||||
DHGRColours.MAGENTA: rgb(221, 0, 51),
|
||||
DHGRColours.BROWN: rgb(136, 85, 34),
|
||||
DHGRColours.ORANGE: rgb(255, 102, 0),
|
||||
DHGRColours.DARK_GREEN: rgb(0, 119, 0),
|
||||
DHGRColours.GREY1: rgb(85, 85, 85),
|
||||
DHGRColours.GREEN: rgb(0, 221, 0),
|
||||
DHGRColours.YELLOW: rgb(255, 255, 0),
|
||||
DHGRColours.DARK_BLUE: rgb(0, 0, 153),
|
||||
DHGRColours.VIOLET: rgb(221, 0, 221),
|
||||
DHGRColours.GREY2: rgb(170, 170, 170),
|
||||
DHGRColours.PINK: rgb(255, 153, 136),
|
||||
DHGRColours.MED_BLUE: rgb(34, 34, 255),
|
||||
DHGRColours.LIGHT_BLUE: rgb(102, 170, 255),
|
||||
DHGRColours.AQUA: rgb(0, 255, 153),
|
||||
DHGRColours.WHITE: rgb(255, 255, 255)
|
||||
HGRColours.BLACK: rgb(0, 0, 0),
|
||||
HGRColours.MAGENTA: rgb(221, 0, 51),
|
||||
HGRColours.BROWN: rgb(136, 85, 34),
|
||||
HGRColours.ORANGE: rgb(255, 102, 0),
|
||||
HGRColours.DARK_GREEN: rgb(0, 119, 0),
|
||||
HGRColours.GREY1: rgb(85, 85, 85),
|
||||
HGRColours.GREEN: rgb(0, 221, 0),
|
||||
HGRColours.YELLOW: rgb(255, 255, 0),
|
||||
HGRColours.DARK_BLUE: rgb(0, 0, 153),
|
||||
HGRColours.VIOLET: rgb(221, 0, 221),
|
||||
HGRColours.GREY2: rgb(170, 170, 170),
|
||||
HGRColours.PINK: rgb(255, 153, 136),
|
||||
HGRColours.MED_BLUE: rgb(34, 34, 255),
|
||||
HGRColours.LIGHT_BLUE: rgb(102, 170, 255),
|
||||
HGRColours.AQUA: rgb(0, 255, 153),
|
||||
HGRColours.WHITE: rgb(255, 255, 255)
|
||||
}
|
||||
|
||||
|
||||
|
@ -3,17 +3,18 @@
|
||||
import bz2
|
||||
import functools
|
||||
import pickle
|
||||
from typing import Union, List
|
||||
from typing import Union, List, Optional, Tuple
|
||||
|
||||
import numpy as np
|
||||
import palette
|
||||
|
||||
import palette as pal
|
||||
|
||||
# Type annotation for cases where we may process either an int or a numpy array.
|
||||
IntOrArray = Union[int, np.ndarray]
|
||||
IntOrArray = Union[np.uint64, np.ndarray]
|
||||
|
||||
|
||||
def y_to_base_addr(y: int, page: int = 0) -> int:
|
||||
"""Maps y coordinate to base address on given screen page"""
|
||||
"""Maps y coordinate to base address on given screen page."""
|
||||
a = y // 64
|
||||
d = y - 64 * a
|
||||
b = d // 8
|
||||
@ -124,62 +125,786 @@ class MemoryMap:
|
||||
self.page_offset[page - self._page_start][offset] = val
|
||||
|
||||
|
||||
class DHGRBitmap:
|
||||
BYTE_MASK32 = [
|
||||
# 3333333222222211111110000000 <- byte 0.3
|
||||
#
|
||||
# 33222222222211111111110000000000 <- bit pos in uint32
|
||||
# 10987654321098765432109876543210
|
||||
# 0000GGGGFFFFEEEEDDDDCCCCBBBBAAAA <- pixel A..G
|
||||
# 3210321032103210321032103210 <- bit pos in A..G pixel
|
||||
0b00000000000000000000000011111111, # byte 0 influences A,B
|
||||
0b00000000000000001111111111110000, # byte 1 influences B,C,D
|
||||
0b00000000111111111111000000000000, # byte 2 influences D,E,F
|
||||
0b00001111111100000000000000000000, # byte 3 influences F,G
|
||||
]
|
||||
class Bitmap:
|
||||
"""Packed bitmap representation of (D)HGR screen memory.
|
||||
|
||||
# How much to right-shift bits after masking to bring into int8/int12 range
|
||||
BYTE_SHIFTS = [0, 4, 12, 20]
|
||||
Maintains a page-based array whose entries contain a packed representation
|
||||
of multiple screen bytes, in a representation that supports efficiently
|
||||
determining the visual effect of storing bytes at arbitrary screen offsets.
|
||||
"""
|
||||
|
||||
# NOTE: See https://github.com/numpy/numpy/issues/2524 and related issues
|
||||
# for why we have to cast things explicitly to np.uint64 - type promotion
|
||||
# to uint64 is broken in numpy :(
|
||||
|
||||
# Name of bitmap type
|
||||
NAME = None # type: str
|
||||
|
||||
# Size of packed representation, consisting of header + body + footer
|
||||
HEADER_BITS = None # type: np.uint64
|
||||
BODY_BITS = None # type: np.uint64
|
||||
FOOTER_BITS = None # type: np.uint64
|
||||
|
||||
# How many bits of packed representation are necessary to determine the
|
||||
# effect of storing a memory byte, e.g. because they influence pixel
|
||||
# colour or are influenced by other bits.
|
||||
MASKED_BITS = None # type: np.uint64
|
||||
|
||||
# How many coloured screen pixels we can extract from MASKED_BITS. Note
|
||||
# that this does not include the last 3 dots represented by the footer,
|
||||
# since we don't have enough information to determine their colour (we
|
||||
# would fall off the end of the 4-bit sliding window)
|
||||
MASKED_DOTS = None # type: np.uint64
|
||||
|
||||
# List of bitmasks for extracting the subset of packed data corresponding
|
||||
# to bits influencing/influenced by a given byte offset. These must be
|
||||
# a contiguous bit mask, i.e. so that after shifting they are enumerated
|
||||
# by 0..2**MASKED_BITS-1
|
||||
BYTE_MASKS = None # type: List[np.uint64]
|
||||
BYTE_SHIFTS = None # type: List[np.uint64]
|
||||
|
||||
# NTSC clock phase at first masked bit
|
||||
PHASES = None # type: List[int]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
palette: pal.Palette,
|
||||
main_memory: MemoryMap,
|
||||
aux_memory: Optional[MemoryMap]
|
||||
):
|
||||
self.palette = palette # type: pal.Palette
|
||||
self.main_memory = main_memory # type: MemoryMap
|
||||
self.aux_memory = aux_memory # type: Optional[MemoryMap]
|
||||
|
||||
self.PACKED_BITS = (
|
||||
self.HEADER_BITS + self.BODY_BITS + self.FOOTER_BITS
|
||||
) # type: np.uint64
|
||||
|
||||
# How many screen bytes we pack into a single scalar
|
||||
self.SCREEN_BYTES = np.uint64(len(self.BYTE_MASKS)) # type: np.uint64
|
||||
|
||||
self.packed = np.empty(
|
||||
shape=(32, 128), dtype=np.uint64) # type: np.ndarray
|
||||
self._pack()
|
||||
|
||||
# TODO: don't leak headers/footers across screen rows. We should be using
|
||||
# x-y representation rather than page-offset
|
||||
|
||||
@staticmethod
|
||||
def _make_header(col: IntOrArray) -> IntOrArray:
|
||||
"""Extract values to use as header of next column."""
|
||||
raise NotImplementedError
|
||||
|
||||
def _body(self) -> np.ndarray:
|
||||
"""Pack related screen bytes into an efficient representation."""
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
def _make_footer(col: IntOrArray) -> IntOrArray:
|
||||
"""Extract values to use as footer of previous column."""
|
||||
raise NotImplementedError
|
||||
|
||||
def _pack(self) -> None:
|
||||
"""Pack MemoryMap into efficient representation for diffing."""
|
||||
|
||||
body = self._body()
|
||||
|
||||
# Prepend last 3 bits of previous odd byte so we can correctly
|
||||
# decode the effective colours at the beginning of the 22-bit tuple
|
||||
prev_col = np.roll(body, 1, axis=1).astype(np.uint64)
|
||||
header = self._make_header(prev_col)
|
||||
# Don't leak header across page boundaries
|
||||
header[:, 0] = 0
|
||||
|
||||
# Append first 3 bits of next even byte so we can correctly
|
||||
# decode the effective colours at the end of the 22-bit tuple
|
||||
next_col = np.roll(body, -1, axis=1).astype(np.uint64)
|
||||
footer = self._make_footer(next_col)
|
||||
# Don't leak footer across page boundaries
|
||||
footer[:, -1] = 0
|
||||
|
||||
self.packed = header ^ body ^ footer
|
||||
|
||||
@staticmethod
|
||||
def masked_update(
|
||||
byte_offset: int,
|
||||
old_value: IntOrArray,
|
||||
new_value: np.uint8) -> IntOrArray:
|
||||
"""Update int/array to store new value at byte_offset in every entry.
|
||||
|
||||
Does not patch up headers/footers of neighbouring columns.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(None)
|
||||
def edit_distances(palette_id: palette.Palette) -> List[np.ndarray]:
|
||||
"""Load edit distance matrices for masked, shifted byte 0..3 values."""
|
||||
data = "transcoder/data/palette_%d_edit_distance.pickle.bz2" % (
|
||||
def byte_offset(page_offset: int, is_aux: bool) -> int:
|
||||
"""Map screen offset for aux/main into offset within packed data."""
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(None)
|
||||
def _byte_offsets(is_aux: bool) -> Tuple[int, int]:
|
||||
"""Return byte offsets within packed data for AUX/MAIN memory."""
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def to_dots(cls, masked_val: int, byte_offset: int) -> int:
|
||||
"""Convert masked representation to bit sequence of display dots."""
|
||||
raise NotImplementedError
|
||||
|
||||
def apply(
|
||||
self,
|
||||
page: int,
|
||||
offset: int,
|
||||
is_aux: bool,
|
||||
value: np.uint8) -> None:
|
||||
"""Update packed representation of changing main/aux memory."""
|
||||
|
||||
byte_offset = self.byte_offset(offset, is_aux)
|
||||
packed_offset = offset // 2
|
||||
|
||||
self.packed[page, packed_offset] = self.masked_update(
|
||||
byte_offset, self.packed[page, packed_offset], value)
|
||||
self._fix_scalar_neighbours(page, packed_offset, byte_offset)
|
||||
|
||||
def _fix_scalar_neighbours(
|
||||
self,
|
||||
page: int,
|
||||
offset: int,
|
||||
byte_offset: int) -> None:
|
||||
"""Fix up column headers/footers when updating a (page, offset)."""
|
||||
|
||||
if byte_offset == 0 and offset > 0:
|
||||
self.packed[page, offset - 1] = self._fix_column_left(
|
||||
self.packed[page, offset - 1],
|
||||
self.packed[page, offset]
|
||||
)
|
||||
elif byte_offset == (self.SCREEN_BYTES - 1) and offset < 127:
|
||||
# Need to also update the 3-bit header of the next column
|
||||
self.packed[page, offset + 1] = self._fix_column_right(
|
||||
self.packed[page, offset + 1],
|
||||
self.packed[page, offset]
|
||||
)
|
||||
|
||||
def _fix_column_left(
|
||||
self,
|
||||
column_left: IntOrArray,
|
||||
column: IntOrArray
|
||||
) -> IntOrArray:
|
||||
"""Patch up the footer of the column to the left."""
|
||||
|
||||
# Mask out footer(s)
|
||||
column_left &= np.uint64(2 ** (self.HEADER_BITS + self.BODY_BITS) - 1)
|
||||
column_left ^= self._make_footer(column)
|
||||
|
||||
return column_left
|
||||
|
||||
def _fix_column_right(
|
||||
self,
|
||||
column_right: IntOrArray,
|
||||
column: IntOrArray
|
||||
) -> IntOrArray:
|
||||
"""Patch up the header of the column to the right."""
|
||||
|
||||
# Mask out header(s)
|
||||
column_right &= np.uint64(
|
||||
(2 ** (self.BODY_BITS + self.FOOTER_BITS) - 1)) << self.HEADER_BITS
|
||||
column_right ^= self._make_header(column)
|
||||
|
||||
return column_right
|
||||
|
||||
def _fix_array_neighbours(
|
||||
self,
|
||||
ary: np.ndarray,
|
||||
byte_offset: int
|
||||
) -> None:
|
||||
"""Fix up column headers/footers for all array entries."""
|
||||
|
||||
# TODO: don't leak header/footer across page boundaries
|
||||
|
||||
# Propagate new value into neighbouring byte headers/footers if
|
||||
# necessary
|
||||
if byte_offset == 0:
|
||||
# Need to also update the footer of the preceding column
|
||||
shifted_left = np.roll(ary, -1, axis=1)
|
||||
self._fix_column_left(ary, shifted_left)
|
||||
|
||||
elif byte_offset == (self.SCREEN_BYTES - 1):
|
||||
# Need to also update the header of the next column
|
||||
shifted_right = np.roll(ary, 1, axis=1)
|
||||
self._fix_column_right(ary, shifted_right)
|
||||
|
||||
@classmethod
|
||||
@functools.lru_cache(None)
|
||||
def edit_distances(cls, palette_id: pal.Palette) -> List[np.ndarray]:
|
||||
"""Load edit distance matrices for masked, shifted byte values."""
|
||||
|
||||
data = "transcoder/data/%s_palette_%d_edit_distance.pickle.bz2" % (
|
||||
cls.NAME,
|
||||
palette_id.value
|
||||
)
|
||||
with bz2.open(data, "rb") as ed:
|
||||
return pickle.load(ed) # type: List[np.ndarray]
|
||||
dist = pickle.load(ed) # type: List[np.ndarray]
|
||||
|
||||
def __init__(self, main_memory: MemoryMap, aux_memory: MemoryMap):
|
||||
self.main_memory = main_memory
|
||||
self.aux_memory = aux_memory
|
||||
# dist is an upper-triangular matrix of edit_distance(a, b)
|
||||
# encoded as dist[(a << N) + b] = edit_distance(a, b)
|
||||
# Because the distance metric is reflexive,
|
||||
# edit_distance(b, a) = edit_distance(a, b)
|
||||
|
||||
self.packed = np.empty(shape=(32, 128), dtype=np.uint32)
|
||||
self._pack()
|
||||
identity = np.arange(2 ** (2 * cls.MASKED_BITS), dtype=np.uint64)
|
||||
# Swap values of form a << N + b to b << N + a
|
||||
transpose = (identity >> cls.MASKED_BITS) + (
|
||||
(identity & np.uint64(2 ** cls.MASKED_BITS - 1)) <<
|
||||
cls.MASKED_BITS)
|
||||
|
||||
def _pack(self) -> None:
|
||||
"""Interleave and pack aux and main memory into 28-bit uint32 array"""
|
||||
for i in range(len(dist)):
|
||||
dist[i][transpose] += dist[i][identity]
|
||||
|
||||
# Palette bit is unused for DHGR so mask it out
|
||||
aux = (self.aux_memory.page_offset & 0x7f).astype(np.uint32)
|
||||
main = (self.main_memory.page_offset & 0x7f).astype(np.uint32)
|
||||
return dist
|
||||
|
||||
# Interleave aux and main memory columns and pack 7-bit masked values
|
||||
# into a 28-bit value. This sequentially encodes 7 4-bit DHGR pixels.
|
||||
# See make_data_tables.py for more discussion about this representation.
|
||||
self.packed = (
|
||||
aux[:, 0::2] +
|
||||
(main[:, 0::2] << 7) +
|
||||
(aux[:, 1::2] << 14) +
|
||||
(main[:, 1::2] << 21)
|
||||
@classmethod
|
||||
def mask_and_shift_data(
|
||||
cls,
|
||||
data: IntOrArray,
|
||||
byte_offset: int) -> IntOrArray:
|
||||
"""Masks and shifts packed data into the MASKED_BITS range."""
|
||||
res = (data & cls.BYTE_MASKS[byte_offset]) >> (
|
||||
cls.BYTE_SHIFTS[byte_offset])
|
||||
assert np.all(res <= 2 ** cls.MASKED_BITS)
|
||||
return res
|
||||
|
||||
# Can't cache all possible values but this seems to give a good enough hit
|
||||
# rate without costing too much memory
|
||||
# TODO: unit tests
|
||||
@functools.lru_cache(10 ** 6)
|
||||
def byte_pair_difference(
|
||||
self,
|
||||
byte_offset: int,
|
||||
old_packed: np.uint64,
|
||||
content: np.uint8
|
||||
) -> np.uint16:
|
||||
"""Compute effect of storing a new content byte within packed data."""
|
||||
|
||||
old_pixels = self.mask_and_shift_data(old_packed, byte_offset)
|
||||
new_pixels = self.mask_and_shift_data(
|
||||
self.masked_update(byte_offset, old_packed, content), byte_offset)
|
||||
|
||||
pair = (old_pixels << self.MASKED_BITS) + new_pixels
|
||||
|
||||
return self.edit_distances(self.palette)[byte_offset][pair]
|
||||
|
||||
def diff_weights(
|
||||
self,
|
||||
source: "Bitmap",
|
||||
is_aux: bool
|
||||
) -> np.ndarray:
|
||||
"""Compute edit distance matrix from source bitmap."""
|
||||
return self._diff_weights(source.packed, is_aux)
|
||||
|
||||
# TODO: unit test
|
||||
def _diff_weights(
|
||||
self,
|
||||
source_packed: np.ndarray,
|
||||
is_aux: bool,
|
||||
content: np.uint8 = None
|
||||
) -> np.ndarray:
|
||||
"""Computes edit distance matrix from source_packed to self.packed
|
||||
|
||||
If content is set, the distance will be computed as if this value
|
||||
was stored into each offset position of source_packed, i.e. to
|
||||
allow evaluating which offsets (if any) should be chosen for storing
|
||||
this content byte.
|
||||
"""
|
||||
|
||||
diff = np.ndarray((32, 256), dtype=np.int)
|
||||
|
||||
offsets = self._byte_offsets(is_aux)
|
||||
|
||||
dists = []
|
||||
for o in offsets:
|
||||
if content is not None:
|
||||
compare_packed = self.masked_update(o, source_packed, content)
|
||||
self._fix_array_neighbours(compare_packed, o)
|
||||
else:
|
||||
compare_packed = source_packed
|
||||
|
||||
# Pixels influenced by byte offset o
|
||||
source_pixels = self.mask_and_shift_data(compare_packed, o)
|
||||
target_pixels = self.mask_and_shift_data(self.packed, o)
|
||||
|
||||
# Concatenate N-bit source and target into 2N-bit values
|
||||
pair = (source_pixels << self.MASKED_BITS) + target_pixels
|
||||
dist = self.edit_distances(self.palette)[o][pair].reshape(
|
||||
pair.shape)
|
||||
dists.append(dist)
|
||||
|
||||
# Interleave even/odd columns
|
||||
diff[:, 0::2] = dists[0]
|
||||
diff[:, 1::2] = dists[1]
|
||||
|
||||
return diff
|
||||
|
||||
def _check_consistency(self):
|
||||
"""Sanity check that headers and footers are consistent."""
|
||||
|
||||
headers = np.roll(self._make_header(self.packed), 1, axis=1).astype(
|
||||
np.uint64)
|
||||
|
||||
footers = np.roll(self._make_footer(self.packed), -1, axis=1).astype(
|
||||
np.uint64)
|
||||
|
||||
mask_hf = np.uint64(0b1110000000000000000000000000000111)
|
||||
|
||||
res = (self.packed ^ headers ^ footers) & mask_hf
|
||||
nz = np.transpose(np.nonzero(res))
|
||||
|
||||
ok = True
|
||||
if nz.size != 0:
|
||||
for p, o in nz.tolist():
|
||||
if o == 0 or o == 127:
|
||||
continue
|
||||
ok = False
|
||||
print(p, o, bin(self.packed[p, o - 1]),
|
||||
bin(headers[p, o]),
|
||||
bin(self.packed[p, o]),
|
||||
bin(self.packed[p, o + 1]), bin(footers[p, o]),
|
||||
bin(res[p, o])
|
||||
)
|
||||
assert ok
|
||||
|
||||
# TODO: unit tests
|
||||
def compute_delta(
|
||||
self,
|
||||
content: int,
|
||||
diff_weights: np.ndarray,
|
||||
is_aux: bool
|
||||
) -> np.ndarray:
|
||||
"""Compute which content stores introduce the least additional error.
|
||||
|
||||
We compute the effect of storing content at all possible offsets
|
||||
within self.packed, and then subtract the previous diff weights.
|
||||
|
||||
Negative values indicate that the new content value is closer to the
|
||||
target than the current content.
|
||||
"""
|
||||
# TODO: use error edit distance?
|
||||
|
||||
new_diff = self._diff_weights(self.packed, is_aux, content)
|
||||
|
||||
# TODO: try different weightings
|
||||
return (new_diff * 5) - diff_weights
|
||||
|
||||
|
||||
class HGRBitmap(Bitmap):
|
||||
"""Packed bitmap representation of HGR screen memory.
|
||||
|
||||
The HGR display is encoded in a somewhat complicated way, so we have to
|
||||
do a bit of work to turn it into a useful format.
|
||||
|
||||
Each screen byte consists of a palette bit (7) and 6 data bits (0..6)
|
||||
|
||||
Each non-palette bit turns on two consecutive display dots, with bit 6
|
||||
repeated a third time. This third dot may or may not be overwritten by the
|
||||
effect of the next byte.
|
||||
|
||||
Turning on the palette bit shifts that byte's dots right by one
|
||||
position.
|
||||
|
||||
Given two neighbouring screen bytes Aaaaaaaa, Bbbbbbbb (at even and odd
|
||||
offsets), where capital letter indicates the position of the palette bit,
|
||||
we use the following 22-bit packed representation:
|
||||
|
||||
2211111111110000000000 <-- bit position in uint22
|
||||
1098765432109876543210
|
||||
ffFbbbbbbbBAaaaaaaaHhh
|
||||
|
||||
h and f are headers/footers derived from the neighbouring screen bytes.
|
||||
|
||||
Since our colour artifact model (see colours.py) uses a sliding 4-bit window
|
||||
onto the dot string, we need to also include a 3-bit header and footer
|
||||
to account for the influence from/on neighbouring bytes, i.e. adjacent
|
||||
packed values. These are just the low/high 2 data bits of the 16-bit
|
||||
body of those neighbouring columns, plus the corresponding palette bit.
|
||||
|
||||
This 22-bit packed representation is sufficient to compute the effects
|
||||
(on pixel colours) of storing a byte at even or odd offsets. From it we
|
||||
can extract the bit stream of displayed HGR dots, and the mapping to pixel
|
||||
colours follows the HGRColours bitmap, see colours.py.
|
||||
|
||||
We put the two A/B palette bits next to each other so that we can
|
||||
mask a contiguous range of bits whose colours influence/are influenced by
|
||||
storing a byte at a given offset.
|
||||
|
||||
We need to mask out bit subsequences of size 3+8+3=14, i.e. the 8-bits
|
||||
corresponding to the byte being stored, plus the neighbouring 3 bits that
|
||||
influence it/are influenced by it.
|
||||
|
||||
Note that the masked representation has the same size for both offsets (
|
||||
14 bits), but different meaning, since the palette bit is in a different
|
||||
position.
|
||||
|
||||
With this masked representation, we can precompute an edit distance for the
|
||||
pixel changes resulting from all possible HGR byte stores, see
|
||||
make_edit_distance.py.
|
||||
|
||||
The edit distance matrix is encoded by concatenating the 14-bit source
|
||||
and target masked values into a 28-bit pair, which indexes into the
|
||||
edit_distance array to give the corresponding edit distance.
|
||||
"""
|
||||
NAME = 'HGR'
|
||||
|
||||
# Size of packed representation, consisting of header + body + footer
|
||||
HEADER_BITS = np.uint64(3)
|
||||
# 2x 8-bit screen bytes
|
||||
BODY_BITS = np.uint64(16)
|
||||
FOOTER_BITS = np.uint64(3)
|
||||
|
||||
# How many bits of packed representation are necessary to determine the
|
||||
# effect of storing a memory byte, e.g. because they influence pixel
|
||||
# colour or are influenced by other bits.
|
||||
MASKED_BITS = np.uint64(14) # 3 + 8 + 3
|
||||
|
||||
# How many coloured screen pixels we can extract from MASKED_BITS. Note
|
||||
# that this does not include the last 3 dots represented by the footer,
|
||||
# since we don't have enough information to determine their colour (we
|
||||
# would fall off the end of the 4-bit sliding window)
|
||||
#
|
||||
# From header: 3 bits (2 HGR pixels but might be shifted right by palette)
|
||||
# From body: 7 bits doubled, plus possible shift from palette bit
|
||||
MASKED_DOTS = np.uint64(18) # 3 + 7 + 7
|
||||
|
||||
# List of bitmasks for extracting the subset of packed data corresponding
|
||||
# to bits influencing/influenced by a given byte offset. These must be
|
||||
# a contiguous bit mask, i.e. so that after shifting they are enumerated
|
||||
# by 0..2**MASKED_BITS-1
|
||||
BYTE_MASKS = [
|
||||
np.uint64(0b0000000011111111111111),
|
||||
np.uint64(0b1111111111111100000000)
|
||||
]
|
||||
BYTE_SHIFTS = [np.uint64(0), np.uint64(8)]
|
||||
|
||||
# NTSC clock phase at first masked bit
|
||||
#
|
||||
# Each HGR byte offset has the same range of uint14 possible
|
||||
# values and nominal colour pixels, but with different initial
|
||||
# phases:
|
||||
# even: 0 (1 at start of 3-bit header)
|
||||
# odd: 2 (3)
|
||||
PHASES = [1, 3]
|
||||
|
||||
def __init__(self, palette: pal.Palette, main_memory: MemoryMap):
|
||||
super(HGRBitmap, self).__init__(palette, main_memory, None)
|
||||
|
||||
@staticmethod
|
||||
def _make_header(col: IntOrArray) -> IntOrArray:
|
||||
"""Extract values to use as header of next column.
|
||||
|
||||
Header format is bits 5,6,0 of previous screen byte
|
||||
i.e. offsets 17, 18, 11 in packed representation
|
||||
"""
|
||||
|
||||
return (
|
||||
(col & np.uint64(0b1 << 11)) >> np.uint64(9) ^ (
|
||||
(col & np.uint64(0b11 << 17)) >> np.uint64(17))
|
||||
)
|
||||
|
||||
def _body(self) -> np.ndarray:
|
||||
"""Pack related screen bytes into an efficient representation.
|
||||
|
||||
Body is of the form:
|
||||
bbbbbbbBAaaaaaaa
|
||||
|
||||
where capital indicates the palette bit.
|
||||
"""
|
||||
|
||||
even = self.main_memory.page_offset[:, 0::2].astype(np.uint64)
|
||||
odd = self.main_memory.page_offset[:, 1::2].astype(np.uint64)
|
||||
|
||||
return (
|
||||
(even << 3) + ((odd & 0x7f) << 12) + ((odd & 0x80) << 4)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _make_footer(col: IntOrArray) -> IntOrArray:
|
||||
"""Extract values to use as footer of previous column.
|
||||
|
||||
Footer format is bits 7,0,1 of next screen byte
|
||||
i.e. offsets 10,3,4 in packed representation
|
||||
"""
|
||||
|
||||
return (
|
||||
(col & np.uint64(0b1 << 10)) >> np.uint64(10) ^ (
|
||||
(col & np.uint64(0b11 << 3)) >> np.uint64(2))
|
||||
) << np.uint64(19)
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(None)
|
||||
def interleaved_byte_offset(x_byte: int, is_aux: bool) -> int:
|
||||
"""Returns 0..3 offset in ByteTuple for a given x_byte and is_aux"""
|
||||
is_odd = x_byte % 2 == 1
|
||||
def byte_offset(page_offset: int, is_aux: bool) -> int:
|
||||
"""Returns 0..1 offset in packed representation for page_offset."""
|
||||
|
||||
assert not is_aux
|
||||
is_odd = page_offset % 2 == 1
|
||||
|
||||
return 1 if is_odd else 0
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(None)
|
||||
def _byte_offsets(is_aux: bool) -> Tuple[int, int]:
|
||||
"""Return byte offsets within packed data for AUX/MAIN memory."""
|
||||
|
||||
assert not is_aux
|
||||
return 0, 1
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(None)
|
||||
def _double_pixels(int7: int) -> int:
|
||||
"""Each bit 0..6 controls two hires dots.
|
||||
|
||||
Input bit 6 is repeated 3 times in case the neighbouring byte is
|
||||
delayed (right-shifted by one dot) due to the palette bit being set,
|
||||
which means the effect of this byte is "extended" by an extra dot.
|
||||
|
||||
Care needs to be taken to mask this out when overwriting.
|
||||
"""
|
||||
double = (
|
||||
# Bit pos 6
|
||||
((int7 & 0x40) << 8) + ((int7 & 0x40) << 7) + (
|
||||
(int7 & 0x40) << 6) +
|
||||
# Bit pos 5
|
||||
((int7 & 0x20) << 6) + ((int7 & 0x20) << 5) +
|
||||
# Bit pos 4
|
||||
((int7 & 0x10) << 5) + ((int7 & 0x10) << 4) +
|
||||
# Bit pos 3
|
||||
((int7 & 0x08) << 4) + ((int7 & 0x08) << 3) +
|
||||
# Bit pos 2
|
||||
((int7 & 0x04) << 3) + ((int7 & 0x04) << 2) +
|
||||
# Bit pos 1
|
||||
((int7 & 0x02) << 2) + ((int7 & 0x02) << 1) +
|
||||
# Bit pos 0
|
||||
((int7 & 0x01) << 1) + (int7 & 0x01)
|
||||
)
|
||||
|
||||
return double
|
||||
|
||||
@classmethod
|
||||
def to_dots(cls, masked_val: int, byte_offset: int) -> int:
|
||||
"""Convert masked representation to bit sequence of display dots.
|
||||
|
||||
Packed representation is of the form:
|
||||
ffFbbbbbbbBAaaaaaaaHhh
|
||||
|
||||
where capital indicates the palette bit.
|
||||
|
||||
Each non-palette bit turns on two display dots, with bit 6 repeated
|
||||
a third time. This may or may not be overwritten by the next byte.
|
||||
|
||||
Turning on the palette bit shifts that byte's dots right by one
|
||||
position.
|
||||
"""
|
||||
|
||||
# Assert 14-bit representation
|
||||
assert (masked_val & (2 ** 14 - 1)) == masked_val
|
||||
|
||||
# Take top 3 bits from header (plus duplicated MSB) not 4, because if it
|
||||
# is palette-shifted then we don't know what is in bit 0
|
||||
h = (masked_val & 0b111) << 5
|
||||
hp = (h & 0x80) >> 7
|
||||
res = cls._double_pixels(h & 0x7f) >> (11 - hp)
|
||||
|
||||
if byte_offset == 0:
|
||||
# Offset 0: bbBAaaaaaaaHhh
|
||||
b = (masked_val >> 3) & 0xff
|
||||
bp = (b & 0x80) >> 7
|
||||
else:
|
||||
# Offset 1: ffFbbbbbbbBAaa
|
||||
bp = (masked_val >> 3) & 0x01
|
||||
b = ((masked_val >> 4) & 0x7f) ^ (bp << 7)
|
||||
|
||||
# Mask out current contents in case we are overwriting the extended
|
||||
# high bit from previous screen byte
|
||||
res &= ~((2 ** 14 - 1) << (3 + bp))
|
||||
res ^= cls._double_pixels(b & 0x7f) << (3 + bp)
|
||||
|
||||
f = ((masked_val >> 12) & 0b11) ^ (
|
||||
(masked_val >> 11) & 0b01) << 7
|
||||
fp = (f & 0x80) >> 7
|
||||
|
||||
# Mask out current contents in case we are overwriting the extended
|
||||
# high bit from previous screen byte
|
||||
res &= ~((2 ** 4 - 1) << (17 + fp))
|
||||
res ^= cls._double_pixels(f & 0x7f) << (17 + fp)
|
||||
return res & (2 ** 21 - 1)
|
||||
|
||||
@staticmethod
|
||||
def masked_update(
|
||||
byte_offset: int,
|
||||
old_value: IntOrArray,
|
||||
new_value: np.uint8) -> IntOrArray:
|
||||
"""Update int/array to store new value at byte_offset in every entry.
|
||||
|
||||
Does not patch up headers/footers of neighbouring columns.
|
||||
"""
|
||||
|
||||
if byte_offset == 0:
|
||||
# Mask out 8-bit value where update will go
|
||||
masked_value = old_value & (~np.uint64(0xff << 3))
|
||||
|
||||
update = np.uint64(new_value) << np.uint64(3)
|
||||
return masked_value ^ update
|
||||
else:
|
||||
# Mask out 8-bit value where update will go
|
||||
masked_value = old_value & (~np.uint64(0xff << 11))
|
||||
|
||||
# shift palette bit into position 0
|
||||
shifted_new_value = (
|
||||
(new_value & 0x7f) << 1) ^ (
|
||||
(new_value & 0x80) >> 7)
|
||||
update = np.uint64(shifted_new_value) << np.uint64(11)
|
||||
return masked_value ^ update
|
||||
|
||||
|
||||
class DHGRBitmap(Bitmap):
|
||||
"""Packed bitmap representation of DHGR screen memory.
|
||||
|
||||
The DHGR display encodes 7 pixels across interleaved 4-byte sequences
|
||||
of AUX and MAIN memory, as follows:
|
||||
|
||||
PBBBAAAA PDDCCCCB PFEEEEDD PGGGGFFF
|
||||
Aux N Main N Aux N+1 Main N+1 (N even)
|
||||
|
||||
Where A..G are the pixels, and P represents the (unused) palette bit.
|
||||
|
||||
This layout makes more sense when written as a (little-endian) 32-bit
|
||||
integer:
|
||||
|
||||
33222222222211111111110000000000 <- bit pos in uint32
|
||||
10987654321098765432109876543210
|
||||
PGGGGFFFPFEEEEDDPDDCCCCBPBBBAAAA
|
||||
|
||||
i.e. apart from the palette bits this is a linear ordering of pixels,
|
||||
when read from LSB to MSB (i.e. right-to-left). i.e. the screen layout
|
||||
order of bits is opposite to the usual binary representation ordering.
|
||||
|
||||
We can simplify things by stripping out the palette bit and packing
|
||||
down to a 28-bit integer representation:
|
||||
|
||||
33222222222211111111110000000000 <- bit pos in uint32
|
||||
10987654321098765432109876543210
|
||||
|
||||
GGGGFFFFEEEEDDDDCCCCBBBBAAAA <- pixel A..G
|
||||
3210321032103210321032103210 <- bit pos in A..G pixel
|
||||
|
||||
3333333222222211111110000000 <- byte offset 0.3
|
||||
|
||||
Since our colour artifact model (see colours.py) uses a sliding 4-bit window
|
||||
onto the dot string, we need to also include a 3-bit header and footer
|
||||
to account for the influence from/on neighbouring bytes, i.e. adjacent
|
||||
packed values. These are just the low/high 3 bits of the 28-bit body of
|
||||
those neighbouring columns.
|
||||
|
||||
This gives a 34-bit packed representation that is sufficient to compute
|
||||
the effects (on pixel colours) of storing a byte at one of the 0..3 offsets.
|
||||
|
||||
Note that this representation is also 1:1 with the actual displayed
|
||||
DHGR dots. The mapping to pixel colours follows the DHGRColours
|
||||
bitmap, see colours.py.
|
||||
|
||||
Because the packed representation is contiguous, we need to mask out bit
|
||||
subsequences of size 3+7+3=13, i.e. the 7-bits corresponding to the
|
||||
byte being stored, plus the neighbouring 3 bits that influence it/are
|
||||
influenced by it.
|
||||
|
||||
With this masked representation, we can precompute an edit distance for the
|
||||
pixel changes resulting from all possible DHGR byte stores, see
|
||||
make_edit_distance.py.
|
||||
|
||||
The edit distance matrix is encoded by concatenating the 13-bit source
|
||||
and target masked values into a 26-bit pair, which indexes into the
|
||||
edit_distance array to give the corresponding edit distance.
|
||||
"""
|
||||
|
||||
NAME = 'DHGR'
|
||||
|
||||
# Packed representation is 3 + 28 + 3 = 34 bits
|
||||
HEADER_BITS = np.uint64(3)
|
||||
BODY_BITS = np.uint64(28)
|
||||
FOOTER_BITS = np.uint64(3)
|
||||
|
||||
# Masked representation selecting the influence of each byte offset
|
||||
MASKED_BITS = np.uint64(13) # 7-bit body + 3-bit header + 3-bit footer
|
||||
|
||||
# Masking is 1:1 with screen dots, but we can't compute the colour of the
|
||||
# last 3 dots because we fall off the end of the 4-bit sliding window
|
||||
MASKED_DOTS = np.uint64(10)
|
||||
|
||||
# 3-bit header + 28-bit body + 3-bit footer
|
||||
BYTE_MASKS = [
|
||||
# 3333222222222211111111110000000000 <- bit pos in uint64
|
||||
# 3210987654321098765432109876543210
|
||||
# tttGGGGFFFFEEEEDDDDCCCCBBBBAAAAhhh <- pixel A..G
|
||||
# 3210321032103210321032103210 <- bit pos in A..G pixel
|
||||
#
|
||||
# 3333333222222211111110000000 <- byte offset 0.3
|
||||
np.uint64(0b0000000000000000000001111111111111), # byte 0 uint13 mask
|
||||
np.uint64(0b0000000000000011111111111110000000), # byte 1 uint13 mask
|
||||
np.uint64(0b0000000111111111111100000000000000), # byte 2 uint13 mask
|
||||
np.uint64(0b1111111111111000000000000000000000), # byte 3 uint13 mask
|
||||
]
|
||||
|
||||
# How much to right-shift bits after masking, to bring into uint13 range
|
||||
BYTE_SHIFTS = [np.uint64(0), np.uint64(7), np.uint64(14), np.uint64(21)]
|
||||
|
||||
# NTSC clock phase at first masked bit
|
||||
#
|
||||
# Each DHGR byte offset has the same range of uint13 possible
|
||||
# values and nominal colour pixels, but with different initial
|
||||
# phases:
|
||||
# AUX 0: 0 (1 at start of 3-bit header)
|
||||
# MAIN 0: 3 (0)
|
||||
# AUX 1: 2 (3)
|
||||
# MAIN 1: 1 (2)
|
||||
PHASES = [1, 0, 3, 2]
|
||||
|
||||
@staticmethod
|
||||
def _make_header(col: IntOrArray) -> IntOrArray:
|
||||
"""Extract upper 3 bits of body for header of next column."""
|
||||
return (col & np.uint64(0b111 << 28)) >> np.uint64(28)
|
||||
|
||||
def _body(self) -> np.ndarray:
|
||||
"""Pack related screen bytes into an efficient representation.
|
||||
|
||||
For DHGR we first strip off the (unused) palette bit to produce
|
||||
7-bit values, then interleave aux and main memory columns and pack
|
||||
these 7-bit values into 28-bits. This sequentially encodes 7 4-bit
|
||||
DHGR pixels, which is the "repeating unit" of the DHGR screen, and
|
||||
in a form that is convenient to operate on.
|
||||
|
||||
We also shift to make room for the 3-bit header.
|
||||
"""
|
||||
|
||||
# Palette bit is unused for DHGR so mask it out
|
||||
aux = (self.aux_memory.page_offset & 0x7f).astype(np.uint64)
|
||||
main = (self.main_memory.page_offset & 0x7f).astype(np.uint64)
|
||||
|
||||
return (
|
||||
(aux[:, 0::2] << 3) +
|
||||
(main[:, 0::2] << 10) +
|
||||
(aux[:, 1::2] << 17) +
|
||||
(main[:, 1::2] << 24)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _make_footer(col: IntOrArray) -> IntOrArray:
|
||||
"""Extract lower 3 bits of body for footer of previous column."""
|
||||
return (col & np.uint64(0b111 << 3)) << np.uint64(28)
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(None)
|
||||
def byte_offset(page_offset: int, is_aux: bool) -> int:
|
||||
"""Returns 0..3 packed byte offset for a given page_offset and is_aux"""
|
||||
|
||||
is_odd = page_offset % 2 == 1
|
||||
if is_aux:
|
||||
if is_odd:
|
||||
return 2
|
||||
@ -190,31 +915,41 @@ class DHGRBitmap:
|
||||
else:
|
||||
return 1
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(None)
|
||||
def _byte_offsets(is_aux: bool) -> Tuple[int, int]:
|
||||
"""Return byte offsets within packed data for AUX/MAIN memory."""
|
||||
|
||||
if is_aux:
|
||||
offsets = (0, 2)
|
||||
else:
|
||||
offsets = (1, 3)
|
||||
|
||||
return offsets
|
||||
|
||||
@classmethod
|
||||
def to_dots(cls, masked_val: int, byte_offset: int) -> int:
|
||||
"""Convert masked representation to bit sequence of display dots.
|
||||
|
||||
For DHGR the 13-bit masked value is already a 13-bit dot sequence
|
||||
so no need to transform it.
|
||||
"""
|
||||
|
||||
return masked_val
|
||||
|
||||
@staticmethod
|
||||
def masked_update(
|
||||
byte_offset: int,
|
||||
old_value: IntOrArray,
|
||||
new_value: int) -> IntOrArray:
|
||||
new_value: np.uint8) -> IntOrArray:
|
||||
"""Update int/array to store new value at byte_offset in every entry.
|
||||
|
||||
Does not patch up headers/footers of neighbouring columns.
|
||||
"""
|
||||
# Mask out 7-bit value where update will go
|
||||
masked_value = old_value & ~(0x7f << (7 * byte_offset))
|
||||
|
||||
update = (new_value & 0x7f) << (7 * byte_offset)
|
||||
masked_value = old_value & (
|
||||
~np.uint64(0x7f << (7 * byte_offset + 3)))
|
||||
|
||||
update = (new_value & np.uint64(0x7f)) << np.uint64(
|
||||
7 * byte_offset + 3)
|
||||
return masked_value ^ update
|
||||
|
||||
def apply(self, page: int, offset: int, is_aux: bool, value: int) -> None:
|
||||
"""Update packed representation of changing main/aux memory."""
|
||||
|
||||
byte_offset = self.interleaved_byte_offset(offset, is_aux)
|
||||
packed_offset = offset // 2
|
||||
|
||||
self.packed[page, packed_offset] = self.masked_update(
|
||||
byte_offset, self.packed[page, packed_offset], value)
|
||||
|
||||
def mask_and_shift_data(
|
||||
self,
|
||||
data: IntOrArray,
|
||||
byte_offset: int) -> IntOrArray:
|
||||
"""Masks and shifts data into the 8 or 12-bit range."""
|
||||
return (data & self.BYTE_MASK32[byte_offset]) >> (
|
||||
self.BYTE_SHIFTS[byte_offset])
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,5 @@
|
||||
"""Encode a sequence of images as an optimized stream of screen changes."""
|
||||
|
||||
import functools
|
||||
import heapq
|
||||
import random
|
||||
from typing import List, Iterator, Tuple
|
||||
@ -15,7 +14,7 @@ from video_mode import VideoMode
|
||||
|
||||
|
||||
class Video:
|
||||
"""Apple II screen memory map encoding a bitmapped frame."""
|
||||
"""Encodes sequence of images into prioritized screen byte changes."""
|
||||
|
||||
CLOCK_SPEED = 1024 * 1024 # type: int
|
||||
|
||||
@ -30,7 +29,7 @@ class Video:
|
||||
self.frame_grabber = frame_grabber # type: FrameGrabber
|
||||
self.ticks_per_second = ticks_per_second # type: float
|
||||
self.ticks_per_frame = (
|
||||
self.ticks_per_second / frame_grabber.input_frame_rate
|
||||
self.ticks_per_second / frame_grabber.input_frame_rate
|
||||
) # type: float
|
||||
self.frame_number = 0 # type: int
|
||||
self.palette = palette # type: Palette
|
||||
@ -42,10 +41,16 @@ class Video:
|
||||
self.aux_memory_map = screen.MemoryMap(
|
||||
screen_page=1) # type: screen.MemoryMap
|
||||
|
||||
self.pixelmap = screen.DHGRBitmap(
|
||||
main_memory=self.memory_map,
|
||||
aux_memory=self.aux_memory_map
|
||||
)
|
||||
self.pixelmap = screen.DHGRBitmap(
|
||||
palette=palette,
|
||||
main_memory=self.memory_map,
|
||||
aux_memory=self.aux_memory_map
|
||||
)
|
||||
else:
|
||||
self.pixelmap = screen.HGRBitmap(
|
||||
palette=palette,
|
||||
main_memory=self.memory_map,
|
||||
)
|
||||
|
||||
# Accumulates pending edit weights across frames
|
||||
self.update_priority = np.zeros((32, 256), dtype=np.int)
|
||||
@ -53,6 +58,8 @@ class Video:
|
||||
self.aux_update_priority = np.zeros((32, 256), dtype=np.int)
|
||||
|
||||
def tick(self, ticks: int) -> bool:
|
||||
"""Keep track of when it is time for a new image frame."""
|
||||
|
||||
if ticks >= (self.ticks_per_frame * self.frame_number):
|
||||
self.frame_number += 1
|
||||
return True
|
||||
@ -63,7 +70,8 @@ class Video:
|
||||
target: screen.MemoryMap,
|
||||
is_aux: bool,
|
||||
) -> Iterator[opcodes.Opcode]:
|
||||
"""Update to match content of frame within provided budget."""
|
||||
"""Converge towards target frame in priority order of edit distance."""
|
||||
|
||||
if is_aux:
|
||||
memory_map = self.aux_memory_map
|
||||
update_priority = self.aux_update_priority
|
||||
@ -71,6 +79,10 @@ class Video:
|
||||
memory_map = self.memory_map
|
||||
update_priority = self.update_priority
|
||||
|
||||
# Make sure nothing is leaking into screen holes
|
||||
assert np.count_nonzero(
|
||||
memory_map.page_offset[screen.SCREEN_HOLES]) == 0
|
||||
|
||||
print("Similarity %f" % (update_priority.mean()))
|
||||
|
||||
yield from self._index_changes(
|
||||
@ -85,20 +97,28 @@ class Video:
|
||||
) -> Iterator[Tuple[int, int, List[int]]]:
|
||||
"""Transform encoded screen to sequence of change tuples."""
|
||||
|
||||
if is_aux:
|
||||
target_pixelmap = screen.DHGRBitmap(
|
||||
main_memory=self.memory_map,
|
||||
aux_memory=target
|
||||
)
|
||||
if self.mode == VideoMode.DHGR:
|
||||
if is_aux:
|
||||
target_pixelmap = screen.DHGRBitmap(
|
||||
main_memory=self.memory_map,
|
||||
aux_memory=target,
|
||||
palette=self.palette
|
||||
)
|
||||
else:
|
||||
target_pixelmap = screen.DHGRBitmap(
|
||||
main_memory=target,
|
||||
aux_memory=self.aux_memory_map,
|
||||
palette=self.palette
|
||||
)
|
||||
else:
|
||||
target_pixelmap = screen.DHGRBitmap(
|
||||
target_pixelmap = screen.HGRBitmap(
|
||||
main_memory=target,
|
||||
aux_memory=self.aux_memory_map
|
||||
palette=self.palette
|
||||
)
|
||||
|
||||
diff_weights = self._diff_weights(
|
||||
self.pixelmap, target_pixelmap, is_aux
|
||||
)
|
||||
diff_weights = target_pixelmap.diff_weights(self.pixelmap, is_aux)
|
||||
# Don't bother storing into screen holes
|
||||
diff_weights[screen.SCREEN_HOLES] = 0
|
||||
|
||||
# Clear any update priority entries that have resolved themselves
|
||||
# with new frame
|
||||
@ -112,6 +132,10 @@ class Video:
|
||||
while priorities:
|
||||
pri, _, page, offset = heapq.heappop(priorities)
|
||||
|
||||
assert not screen.SCREEN_HOLES[page, offset], (
|
||||
"Attempted to store into screen hole at (%d, %d)" % (
|
||||
page, offset))
|
||||
|
||||
# Check whether we've already cleared this diff while processing
|
||||
# an earlier opcode
|
||||
if update_priority[page, offset] == 0:
|
||||
@ -119,7 +143,9 @@ class Video:
|
||||
|
||||
offsets = [offset]
|
||||
content = target.page_offset[page, offset]
|
||||
assert content < 0x80 # DHGR palette bit not expected to be set
|
||||
if self.mode == VideoMode.DHGR:
|
||||
# DHGR palette bit not expected to be set
|
||||
assert content < 0x80
|
||||
|
||||
# Clear priority for the offset we're emitting
|
||||
update_priority[page, offset] = 0
|
||||
@ -146,9 +172,12 @@ class Video:
|
||||
is_aux
|
||||
):
|
||||
assert o != offset
|
||||
assert not screen.SCREEN_HOLES[page, o], (
|
||||
"Attempted to store into screen hole at (%d, %d)" % (
|
||||
page, o))
|
||||
|
||||
if update_priority[page, o] == 0:
|
||||
# print("Skipping page=%d, offset=%d" % (page, o))
|
||||
# Someone already resolved this diff.
|
||||
continue
|
||||
|
||||
# Make sure we don't end up considering this (page, offset)
|
||||
@ -158,14 +187,14 @@ class Video:
|
||||
for cd in content_deltas.values():
|
||||
cd[page, o] = 0
|
||||
|
||||
byte_offset = target_pixelmap.interleaved_byte_offset(o, is_aux)
|
||||
byte_offset = target_pixelmap.byte_offset(o, is_aux)
|
||||
old_packed = target_pixelmap.packed[page, o // 2]
|
||||
|
||||
p = self._byte_pair_difference(
|
||||
target_pixelmap, byte_offset, old_packed, content)
|
||||
p = target_pixelmap.byte_pair_difference(
|
||||
byte_offset, old_packed, content)
|
||||
|
||||
# Update priority for the offset we're emitting
|
||||
update_priority[page, o] = p # 0
|
||||
update_priority[page, o] = p
|
||||
|
||||
source.page_offset[page, o] = content
|
||||
self.pixelmap.apply(page, o, is_aux, content)
|
||||
@ -175,7 +204,7 @@ class Video:
|
||||
# heap in case we can get back to fixing it exactly
|
||||
# during this frame. Otherwise we'll get to it later.
|
||||
heapq.heappush(
|
||||
priorities, (-p, random.getrandbits(16), page, o))
|
||||
priorities, (-p, random.getrandbits(8), page, o))
|
||||
|
||||
offsets.append(o)
|
||||
if len(offsets) == 3:
|
||||
@ -186,19 +215,30 @@ class Video:
|
||||
offsets.append(offsets[0])
|
||||
yield (page + 32, content, offsets)
|
||||
|
||||
# TODO: there is still a bug causing residual diffs when we have
|
||||
# apparently run out of work to do
|
||||
# # TODO: there is still a bug causing residual diffs when we have
|
||||
# # apparently run out of work to do
|
||||
if not np.array_equal(source.page_offset, target.page_offset):
|
||||
diffs = np.nonzero(source.page_offset != target.page_offset)
|
||||
for i in range(len(diffs[0])):
|
||||
diff_p = diffs[0][i]
|
||||
diff_o = diffs[1][i]
|
||||
|
||||
# For HGR, 0x00 or 0x7f may be visually equivalent to the same
|
||||
# bytes with high bit set (depending on neighbours), so skip
|
||||
# them
|
||||
if (source.page_offset[diff_p, diff_o] & 0x7f) == 0 and \
|
||||
(target.page_offset[diff_p, diff_o] & 0x7f) == 0:
|
||||
continue
|
||||
|
||||
if (source.page_offset[diff_p, diff_o] & 0x7f) == 0x7f and \
|
||||
(target.page_offset[diff_p, diff_o] & 0x7f) == 0x7f:
|
||||
continue
|
||||
|
||||
print("Diff at (%d, %d): %d != %d" % (
|
||||
diff_p, diff_o, source.page_offset[diff_p, diff_o],
|
||||
target.page_offset[diff_p, diff_o]
|
||||
))
|
||||
# assert False
|
||||
# assert False
|
||||
|
||||
# If we run out of things to do, pad forever
|
||||
content = target.page_offset[0, 0]
|
||||
@ -207,11 +247,15 @@ class Video:
|
||||
|
||||
@staticmethod
|
||||
def _heapify_priorities(update_priority: np.array) -> List:
|
||||
"""Build priority queue of (page, offset) ordered by update priority."""
|
||||
|
||||
# Use numpy vectorization to efficiently compute the list of
|
||||
# (priority, random nonce, page, offset) tuples to be heapified.
|
||||
pages, offsets = update_priority.nonzero()
|
||||
priorities = [tuple(data) for data in np.stack((
|
||||
-update_priority[pages, offsets],
|
||||
# Don't use deterministic order for page, offset
|
||||
np.random.randint(0, 2**8, size=pages.shape[0]),
|
||||
np.random.randint(0, 2 ** 8, size=pages.shape[0]),
|
||||
pages,
|
||||
offsets)
|
||||
).T.tolist()]
|
||||
@ -219,145 +263,19 @@ class Video:
|
||||
heapq.heapify(priorities)
|
||||
return priorities
|
||||
|
||||
def _diff_weights(
|
||||
self,
|
||||
source: screen.DHGRBitmap,
|
||||
target: screen.DHGRBitmap,
|
||||
is_aux: bool
|
||||
):
|
||||
diff = np.ndarray((32, 256), dtype=np.int)
|
||||
|
||||
if is_aux:
|
||||
# Pixels influenced by byte offset 0
|
||||
source_pixels0 = source.mask_and_shift_data(source.packed, 0)
|
||||
target_pixels0 = target.mask_and_shift_data(target.packed, 0)
|
||||
|
||||
# Concatenate 8-bit source and target into 16-bit values
|
||||
pair0 = (source_pixels0 << 8) + target_pixels0
|
||||
dist0 = source.edit_distances(self.palette)[0][pair0].reshape(
|
||||
pair0.shape)
|
||||
|
||||
# Pixels influenced by byte offset 2
|
||||
source_pixels2 = source.mask_and_shift_data(source.packed, 2)
|
||||
target_pixels2 = target.mask_and_shift_data(target.packed, 2)
|
||||
# Concatenate 12-bit source and target into 24-bit values
|
||||
pair2 = (source_pixels2 << 12) + target_pixels2
|
||||
dist2 = source.edit_distances(self.palette)[2][pair2].reshape(
|
||||
pair2.shape)
|
||||
|
||||
diff[:, 0::2] = dist0
|
||||
diff[:, 1::2] = dist2
|
||||
|
||||
else:
|
||||
# Pixels influenced by byte offset 1
|
||||
source_pixels1 = source.mask_and_shift_data(source.packed, 1)
|
||||
target_pixels1 = target.mask_and_shift_data(target.packed, 1)
|
||||
pair1 = (source_pixels1 << 12) + target_pixels1
|
||||
dist1 = source.edit_distances(self.palette)[1][pair1].reshape(
|
||||
pair1.shape)
|
||||
|
||||
# Pixels influenced by byte offset 3
|
||||
source_pixels3 = source.mask_and_shift_data(source.packed, 3)
|
||||
target_pixels3 = target.mask_and_shift_data(target.packed, 3)
|
||||
pair3 = (source_pixels3 << 8) + target_pixels3
|
||||
dist3 = source.edit_distances(self.palette)[3][pair3].reshape(
|
||||
pair3.shape)
|
||||
|
||||
diff[:, 0::2] = dist1
|
||||
diff[:, 1::2] = dist3
|
||||
|
||||
return diff
|
||||
|
||||
@functools.lru_cache(None)
|
||||
def _byte_pair_difference(
|
||||
self,
|
||||
target_pixelmap,
|
||||
byte_offset,
|
||||
old_packed,
|
||||
content
|
||||
):
|
||||
|
||||
old_pixels = target_pixelmap.mask_and_shift_data(
|
||||
old_packed, byte_offset)
|
||||
new_pixels = target_pixelmap.mask_and_shift_data(
|
||||
target_pixelmap.masked_update(
|
||||
byte_offset, old_packed, content), byte_offset)
|
||||
|
||||
if byte_offset == 0 or byte_offset == 3:
|
||||
pair = (old_pixels << 8) + new_pixels
|
||||
else:
|
||||
pair = (old_pixels << 12) + new_pixels
|
||||
|
||||
p = target_pixelmap.edit_distances(self.palette)[byte_offset][pair]
|
||||
|
||||
return p
|
||||
|
||||
def _compute_delta(
|
||||
self,
|
||||
content: int,
|
||||
target: screen.DHGRBitmap,
|
||||
old,
|
||||
is_aux: bool
|
||||
):
|
||||
diff = np.ndarray((32, 256), dtype=np.int)
|
||||
|
||||
# TODO: use error edit distance
|
||||
|
||||
if is_aux:
|
||||
# Pixels influenced by byte offset 0
|
||||
source_pixels0 = target.mask_and_shift_data(
|
||||
target.masked_update(0, target.packed, content), 0)
|
||||
target_pixels0 = target.mask_and_shift_data(target.packed, 0)
|
||||
|
||||
# Concatenate 8-bit source and target into 16-bit values
|
||||
pair0 = (source_pixels0 << 8) + target_pixels0
|
||||
dist0 = target.edit_distances(self.palette)[0][pair0].reshape(
|
||||
pair0.shape)
|
||||
|
||||
# Pixels influenced by byte offset 2
|
||||
source_pixels2 = target.mask_and_shift_data(
|
||||
target.masked_update(2, target.packed, content), 2)
|
||||
target_pixels2 = target.mask_and_shift_data(target.packed, 2)
|
||||
# Concatenate 12-bit source and target into 24-bit values
|
||||
pair2 = (source_pixels2 << 12) + target_pixels2
|
||||
dist2 = target.edit_distances(self.palette)[2][pair2].reshape(
|
||||
pair2.shape)
|
||||
|
||||
diff[:, 0::2] = dist0
|
||||
diff[:, 1::2] = dist2
|
||||
|
||||
else:
|
||||
# Pixels influenced by byte offset 1
|
||||
source_pixels1 = target.mask_and_shift_data(
|
||||
target.masked_update(1, target.packed, content), 1)
|
||||
target_pixels1 = target.mask_and_shift_data(target.packed, 1)
|
||||
pair1 = (source_pixels1 << 12) + target_pixels1
|
||||
dist1 = target.edit_distances(self.palette)[1][pair1].reshape(
|
||||
pair1.shape)
|
||||
|
||||
# Pixels influenced by byte offset 3
|
||||
source_pixels3 = target.mask_and_shift_data(
|
||||
target.masked_update(3, target.packed, content), 3)
|
||||
target_pixels3 = target.mask_and_shift_data(target.packed, 3)
|
||||
pair3 = (source_pixels3 << 8) + target_pixels3
|
||||
dist3 = target.edit_distances(self.palette)[3][pair3].reshape(
|
||||
pair3.shape)
|
||||
|
||||
diff[:, 0::2] = dist1
|
||||
diff[:, 1::2] = dist3
|
||||
|
||||
# TODO: try different weightings
|
||||
return (diff * 5) - old
|
||||
|
||||
_OFFSETS = np.arange(256)
|
||||
|
||||
def _compute_error(self, page, content, target_pixelmap, old_error,
|
||||
def _compute_error(self, page, content, target_pixelmap, diff_weights,
|
||||
content_deltas, is_aux):
|
||||
"""Build priority queue of other offsets at which to store content.
|
||||
|
||||
Ordered by offsets which are closest to the target content value.
|
||||
"""
|
||||
# TODO: move this up into parent
|
||||
delta_screen = content_deltas.get(content)
|
||||
if delta_screen is None:
|
||||
delta_screen = self._compute_delta(
|
||||
content, target_pixelmap, old_error, is_aux)
|
||||
delta_screen = target_pixelmap.compute_delta(
|
||||
content, diff_weights, is_aux)
|
||||
content_deltas[content] = delta_screen
|
||||
|
||||
delta_page = delta_screen[page]
|
||||
@ -366,7 +284,7 @@ class Video:
|
||||
priorities = delta_page[cond]
|
||||
|
||||
deltas = [
|
||||
(priorities[i], random.getrandbits(16), candidate_offsets[i])
|
||||
(priorities[i], random.getrandbits(8), candidate_offsets[i])
|
||||
for i in range(len(candidate_offsets))
|
||||
]
|
||||
heapq.heapify(deltas)
|
||||
@ -374,6 +292,6 @@ class Video:
|
||||
while deltas:
|
||||
pri, _, o = heapq.heappop(deltas)
|
||||
assert pri < 0
|
||||
assert o < 255
|
||||
assert o <= 255
|
||||
|
||||
yield -pri, o
|
||||
|
@ -4,5 +4,5 @@ import enum
|
||||
|
||||
|
||||
class VideoMode(enum.Enum):
|
||||
HGR = 0
|
||||
DHGR = 1
|
||||
HGR = 0 # Hi-Res
|
||||
DHGR = 1 # Double Hi-Res
|
||||
|
@ -21,22 +21,23 @@ class TestVideo(unittest.TestCase):
|
||||
frame.page_offset[0, 1] = 0b1010101
|
||||
|
||||
target_pixelmap = screen.DHGRBitmap(
|
||||
palette=palette.Palette.NTSC,
|
||||
main_memory=v.memory_map,
|
||||
aux_memory=frame
|
||||
)
|
||||
self.assertEqual(
|
||||
0b0000000101010100000001111111,
|
||||
0b0000000000101010100000001111111000,
|
||||
target_pixelmap.packed[0, 0])
|
||||
|
||||
diff = v._diff_weights(v.pixelmap, target_pixelmap, is_aux=True)
|
||||
|
||||
pal = palette.NTSCPalette
|
||||
|
||||
# Expect byte 0 to map to 0b00000000 01111111
|
||||
expect0 = target_pixelmap.edit_distances(pal.ID)[0][0b0000000001111111]
|
||||
diff = target_pixelmap.diff_weights(v.pixelmap, is_aux=True)
|
||||
|
||||
# Expect byte 2 to map to 0b000000000000 000101010100
|
||||
expect2 = target_pixelmap.edit_distances(pal.ID)[2][0b000101010100]
|
||||
# Expect byte 0 to map to 0b0001111111000
|
||||
expect0 = target_pixelmap.edit_distances(pal.ID)[0][0b0001111111000]
|
||||
|
||||
# Expect byte 2 to map to 0b0001010101000
|
||||
expect2 = target_pixelmap.edit_distances(pal.ID)[2][0b0001010101000]
|
||||
|
||||
self.assertEqual(expect0, diff[0, 0])
|
||||
self.assertEqual(expect2, diff[0, 1])
|
||||
@ -45,7 +46,7 @@ class TestVideo(unittest.TestCase):
|
||||
v.aux_memory_map.page_offset = frame.page_offset
|
||||
v.pixelmap._pack()
|
||||
self.assertEqual(
|
||||
0b0000000101010100000001111111,
|
||||
0b0000000000101010100000001111111000,
|
||||
v.pixelmap.packed[0, 0]
|
||||
)
|
||||
|
||||
@ -56,21 +57,23 @@ class TestVideo(unittest.TestCase):
|
||||
|
||||
target_pixelmap = screen.DHGRBitmap(
|
||||
main_memory=v.memory_map,
|
||||
aux_memory=frame
|
||||
aux_memory=frame,
|
||||
palette=pal.ID
|
||||
)
|
||||
self.assertEqual(
|
||||
0b0000000011011000000001101101,
|
||||
0b0000000000011011000000001101101000,
|
||||
target_pixelmap.packed[0, 0]
|
||||
)
|
||||
|
||||
diff = v._diff_weights(v.pixelmap, target_pixelmap, is_aux=True)
|
||||
diff = target_pixelmap.diff_weights(v.pixelmap, is_aux=True)
|
||||
|
||||
# Expect byte 0 to map to 0b01111111 01101101
|
||||
expect0 = target_pixelmap.edit_distances(pal.ID)[0][0b0111111101101101]
|
||||
# Masked offset 0 changes from 0001111111000 to 0001101101000
|
||||
expect0 = target_pixelmap.edit_distances(pal.ID)[0][
|
||||
0b00011111110000001101101000]
|
||||
|
||||
# Expect byte 2 to map to 0b000101010100 000011011000
|
||||
# Masked offset 2 changes from 0001010101000 to 0000110110000
|
||||
expect2 = target_pixelmap.edit_distances(pal.ID)[2][
|
||||
0b0000101010100000011011000]
|
||||
0b00010101010000000110110000]
|
||||
|
||||
self.assertEqual(expect0, diff[0, 0])
|
||||
self.assertEqual(expect2, diff[0, 1])
|
||||
|
Loading…
x
Reference in New Issue
Block a user