-
Notifications
You must be signed in to change notification settings - Fork 6
/
animate_delta.go
2154 lines (1905 loc) · 42 KB
/
animate_delta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package animate
import (
"bytes"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"os"
"path/filepath"
"sync"
"text/template"
"github.com/jeromelesaux/martine/config"
"github.com/jeromelesaux/martine/constants"
"github.com/jeromelesaux/martine/export/amsdos"
"github.com/jeromelesaux/martine/export/ascii"
"github.com/jeromelesaux/martine/export/compression"
"github.com/jeromelesaux/martine/export/png"
"github.com/jeromelesaux/martine/gfx"
"github.com/jeromelesaux/martine/gfx/errors"
"github.com/jeromelesaux/martine/gfx/transformation"
"github.com/jeromelesaux/martine/log"
zx0 "github.com/jeromelesaux/zx0/encode"
)
type DeltaExportFormat int
var (
DeltaExportV1 DeltaExportFormat = 1
DeltaExportV2 DeltaExportFormat = 2
)
// nolint: funlen
func DeltaPackingMemory(images []image.Image, cfg *config.MartineConfig, initialAddress uint16, mode uint8) ([]*transformation.DeltaCollection, [][]byte, color.Palette, error) {
var isSprite bool = true
maxImages := 22
var pad int = 1
var err error
var palette color.Palette
if !cfg.CustomDimension && !cfg.SpriteHard {
isSprite = false
}
if len(images) <= 1 {
return nil, nil, palette, fmt.Errorf("need more than one image to proceed")
}
if len(images) > maxImages {
log.GetLogger().Error("Warning gif exceed 30 images. Will corrupt the number of images.")
pad = len(images) / maxImages
}
rawImages := make([][]byte, 0)
deltaData := make([]*transformation.DeltaCollection, 0)
var raw []byte
// now transform images as win or scr
log.GetLogger().Info("Let's go transform images files in win or scr\n")
_, _, palette, _, err = gfx.ApplyOneImage(images[0], cfg, int(mode), palette, mode)
if err != nil {
return nil, nil, palette, err
}
for i := 0; i < len(images); i += pad {
in := images[i]
raw, _, _, _, err = gfx.ApplyOneImage(in, cfg, int(mode), palette, mode)
if err != nil {
return nil, nil, palette, err
}
rawImages = append(rawImages, raw)
log.GetLogger().Info("Image [%d] proceed\n", i)
}
lineOctetsWidth := cfg.LineWidth
x0, y0, err := transformation.CpcCoordinates(initialAddress, 0xC000, lineOctetsWidth)
if err != nil {
log.GetLogger().Error("error while computing cpc coordinates :%v\n", err)
}
log.GetLogger().Info("Let's go deltapacking raw images\n")
realSize := &constants.Size{Width: cfg.Size.Width, Height: cfg.Size.Height}
if isSprite {
realSize.Width = realSize.ModeWidth(mode)
}
var lastImage []byte
for i := 0; i < len(rawImages)-1; i++ {
log.GetLogger().Info("Compare image [%d] with [%d] ", i, i+1)
d1 := rawImages[i]
d2 := rawImages[i+1]
if len(d1) != len(d2) {
return nil, nil, palette, errors.ErrorSizeDiffers
}
lastImage = d2
dc := transformation.Delta(d1, d2, isSprite, *realSize, mode, uint16(x0), uint16(y0), lineOctetsWidth)
deltaData = append(deltaData, dc)
log.GetLogger().Info("%d bytes differ from the both images\n", len(dc.Items))
}
log.GetLogger().Info("Compare image [%d] with [%d] ", len(rawImages)-1, 0)
d1 := lastImage
d2 := rawImages[0]
dc := transformation.Delta(d1, d2, isSprite, *realSize, mode, uint16(x0), uint16(y0), lineOctetsWidth)
deltaData = append(deltaData, dc)
log.GetLogger().Info("%d bytes differ from the both images\n", len(dc.Items))
return deltaData, rawImages, palette, nil
}
// nolint: funlen
func DeltaPacking(gitFilepath string, cfg *config.MartineConfig, initialAddress uint16, mode uint8, exportVersion DeltaExportFormat) error {
isSprite := true
maxImages := 22
if !cfg.CustomDimension && !cfg.SpriteHard {
isSprite = false
}
fr, err := os.Open(gitFilepath)
if err != nil {
return err
}
defer fr.Close()
gifImages, err := gif.DecodeAll(fr)
if err != nil {
return err
}
images := ConvertToImage(*gifImages)
var pad int = 1
if len(images) <= 1 {
return fmt.Errorf("need more than one image to proceed")
}
if len(images) > maxImages {
log.GetLogger().Error("Warning gif exceed 30 images. Will corrupt the number of images.")
pad = len(images) / maxImages
}
rawImages := make([][]byte, 0)
deltaData := make([]*transformation.DeltaCollection, 0)
var palette color.Palette
var raw []byte
// now transform images as win or scr
log.GetLogger().Info("Let's go transform images files in win or scr\n")
if cfg.FilloutGif {
imgs := filloutGif(*gifImages, cfg)
_, _, palette, _, err = gfx.ApplyOneImage(imgs[0], cfg, int(mode), palette, mode)
if err != nil {
return err
}
for i := 0; i < len(imgs); i += pad {
in := imgs[i]
raw, _, _, _, err = gfx.ApplyOneImage(in, cfg, int(mode), palette, mode)
if err != nil {
return err
}
rawImages = append(rawImages, raw)
log.GetLogger().Info("Image [%d] proceed\n", i)
}
} else {
_, _, palette, _, err = gfx.ApplyOneImage(images[0], cfg, int(mode), palette, mode)
if err != nil {
return err
}
for i := 0; i < len(images); i += pad {
in := images[i]
raw, _, _, _, err = gfx.ApplyOneImage(in, cfg, int(mode), palette, mode)
if err != nil {
return err
}
err = png.Png(cfg.OutputPath+fmt.Sprintf("/%.2d.png", i), in)
if err != nil {
return err
}
rawImages = append(rawImages, raw)
log.GetLogger().Info("Image [%d] proceed\n", i)
}
}
lineOctetsWidth := cfg.LineWidth
x0, y0, err := transformation.CpcCoordinates(initialAddress, 0xC000, lineOctetsWidth)
if err != nil {
log.GetLogger().Error("error while computing cpc coordinates :%v\n", err)
}
log.GetLogger().Info("Let's go deltapacking raw images\n")
realSize := &constants.Size{Width: cfg.Size.Width, Height: cfg.Size.Height}
realSize.Width = realSize.ModeWidth(mode)
var lastImage []byte
for i := 0; i < len(rawImages)-1; i++ {
log.GetLogger().Info("Compare image [%d] with [%d] ", i, i+1)
d1 := rawImages[i]
d2 := rawImages[i+1]
if len(d1) != len(d2) {
return errors.ErrorSizeDiffers
}
lastImage = d2
dc := transformation.Delta(d1, d2, isSprite, *realSize, mode, uint16(x0), uint16(y0), lineOctetsWidth)
deltaData = append(deltaData, dc)
log.GetLogger().Info("%d bytes differ from the both images\n", len(dc.Items))
}
log.GetLogger().Info("Compare image [%d] with [%d] ", len(rawImages)-1, 0)
d1 := lastImage
d2 := rawImages[0]
dc := transformation.Delta(d1, d2, isSprite, *realSize, mode, uint16(x0), uint16(y0), lineOctetsWidth)
deltaData = append(deltaData, dc)
log.GetLogger().Info("%d bytes differ from the both images\n", len(dc.Items))
filename := cfg.OsFilename(".asm")
_, err = ExportDeltaAnimate(rawImages[0], deltaData, palette, isSprite, cfg, initialAddress, mode, cfg.OutputPath+string(filepath.Separator)+filename, exportVersion)
return err
}
func ConvertToImage(g gif.GIF) []*image.NRGBA {
c := make([]*image.NRGBA, 0)
imgRect := image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: g.Config.Width, Y: g.Config.Height}}
origImg := image.NewRGBA(imgRect)
draw.Draw(origImg, g.Image[0].Bounds(), g.Image[0], g.Image[0].Bounds().Min, 0)
c = append(c, (*image.NRGBA)(origImg))
previousImg := origImg
for i := 1; i < len(g.Image); i++ {
img := image.NewRGBA(imgRect)
draw.Draw(img, previousImg.Bounds(), previousImg, previousImg.Bounds().Min, draw.Over)
currImg := g.Image[i]
draw.Draw(img, currImg.Bounds(), currImg, currImg.Bounds().Min, draw.Over)
c = append(c, (*image.NRGBA)(img))
previousImg = img
}
return c
}
func filloutGif(g gif.GIF, cfg *config.MartineConfig) []image.Image {
c := make([]image.Image, 0)
width := g.Image[0].Bounds().Max.X
height := g.Image[0].Bounds().Max.Y
reference := image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(reference, reference.Bounds(), g.Image[0], image.Point{0, 0}, draw.Src)
for i := 1; i < len(g.Image)-1; i++ {
in := g.Image[i]
draw.Draw(reference, reference.Bounds(), in, image.Point{0, 0}, draw.Over)
img := image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(img, img.Bounds(), reference, image.Point{0, 0}, draw.Over)
c = append(c, img)
}
return c
}
// nolint: funlen, gocognit
func ExportDeltaAnimate(
imageReference []byte,
delta []*transformation.DeltaCollection,
palette color.Palette,
isSprite bool,
cfg *config.MartineConfig,
initialAddress uint16,
mode uint8,
filename string,
exportVersion DeltaExportFormat,
) (string, error) {
an := AnimateValues{
InitialAddress: fmt.Sprintf("#%.4x", initialAddress),
Palette: palette,
Mode: int(mode),
LigneLarge: fmt.Sprintf("#%.4x", 0xC000+cfg.LineWidth),
Haut: fmt.Sprintf("%d", cfg.Size.Height),
Large: fmt.Sprintf("%d", cfg.Size.ModeWidth(mode)),
Image: imageReference,
Type: AnimateExportType{
Compress: cfg.Compression != compression.NONE,
IsSprite: isSprite,
CPCPlus: cfg.CpcPlus,
},
}
data := make([][]byte, 0)
for _, v := range delta {
if v.OccurencePerFrame == 0 {
continue
}
if exportVersion == DeltaExportV2 {
v2 := &transformation.DeltaCollectionV2{DeltaCollection: v}
d, err := v2.Marshall()
if err != nil {
return "", err
}
data = append(data, d)
} else {
d, err := v.Marshall()
if err != nil {
return "", err
}
data = append(data, d)
}
}
an.Delta = data
var sourceCode string
if !isSprite {
if cfg.Compression != compression.NONE {
sourceCode = deltaScreenCompressCodeDelta
if cfg.CpcPlus {
sourceCode = deltaScreenCompressCodeDeltaPlus
} else {
if exportVersion == DeltaExportV2 {
sourceCode = deltaScreenCompressCodeDeltaV2
}
}
} else {
sourceCode = deltaScreenCodeDelta
if cfg.CpcPlus {
sourceCode = deltaScreenCodeDeltaPlus
} else {
if exportVersion == DeltaExportV2 {
sourceCode = deltaScreenCodeDeltaV2
}
}
}
} else {
sourceCode = deltaCodeDelta
}
var buf bytes.Buffer
temp := template.Must(template.New("code").Parse(sourceCode))
err := temp.Execute(&buf, an)
if err != nil {
return "", err
}
fmt.Println(buf.String())
code := buf.String()
if cfg.Compression != compression.NONE {
code += "\nbuffer:\n"
}
code += "\nend\n"
code += "\nsave'disc.bin',#200, end - start,DSK,'delta.dsk'"
if filename != "" {
err = amsdos.SaveStringOSFile(filename, code)
if err != nil {
return "", err
}
return code, nil
}
return code, nil
}
type AnimateExportType struct {
Compress bool
CPCPlus bool
IsSprite bool
}
var AnimateTemplates = map[AnimateExportType]string{
{Compress: false, CPCPlus: false, IsSprite: false}: depackRoutine,
}
type AnimateValues struct {
Type AnimateExportType
InitialAddress string
Large string
Haut string
LigneLarge string
Mode int
Image []byte
Delta [][]byte
Palette color.Palette
}
func (a AnimateValues) DisplayCode() string {
var code string
var mu sync.Mutex
if a.Type.Compress {
log.GetLogger().Info("Using Zx0 cruncher")
d := zx0.Encode(a.Image)
mu.Lock()
code += "sprite:\n"
code += ascii.FormatAssemblyDatabyte(d, "\n")
mu.Unlock()
} else {
code += "sprite:\n"
code += ascii.FormatAssemblyDatabyte(a.Image, "\n")
}
ascii.ByteToken = "db"
if a.Type.Compress {
var wg sync.WaitGroup
for i, v := range a.Delta {
wg.Add(1)
go func(indice int, data []byte) {
defer wg.Done()
log.GetLogger().Info("Using Zx0 cruncher")
d := zx0.Encode(data)
mu.Lock()
code += fmt.Sprintf("delta%.2d:\n", indice) // add semaphore on code string
code += ascii.FormatAssemblyDatabyte(d, "\n")
mu.Unlock()
}(i, v)
}
wg.Wait()
} else {
for i, v := range a.Delta {
code += fmt.Sprintf("delta%.2d:\n", i)
code += ascii.FormatAssemblyDatabyte(v, "\n")
}
}
return code
}
func (a AnimateValues) TableDelta() string {
var code string
deltaIndexes := make([]string, 0)
for i := range a.Delta {
deltaIndexes = append(deltaIndexes, fmt.Sprintf("delta%.2d", i))
}
ascii.ByteToken = "dw"
code += ascii.FormatAssemblyString(deltaIndexes, "\n")
return code
}
func (a AnimateValues) DisplayPalette() string {
var code string
ascii.ByteToken = "db"
code += "db "
if a.Type.CPCPlus {
switch a.Mode {
case 0:
code += "#8C\ndb "
case 1:
code += "#8D\ndb "
case 2:
code += "#8E\ndb "
}
code += ascii.FormatAssemblyCPCPlusPalette(a.Palette, "\n")
} else {
code += ascii.FormatAssemblyBasicPalette(a.Palette, "\n")
}
return code
}
var deltaScreenCodeDeltaV2 = `
;--- dimensions du sprite ----
large equ {{ .Large }}
haut equ {{ .Haut }}
loadingaddress equ #200
linewidth equ {{ .LigneLarge }}
nbdelta equ {{ .Delta }}
nbcolors equ {{ .Palette }}
;-----------------------------
org loadingaddress
run loadingaddress
;-----------------------------
start
;--- selection du mode ---------
ld a,{{ .Mode }}
call #BC0E
;-------------------------------
;--- gestion de la palette ----
call palettefirmware
;------------------------------
call xvbl
;--- affichage du sprite initiale --
; affichage du premier ecran
ld de,#C000
ld hl,sprite
ldir
;------------------------------------
mainloop ; routine pour afficher les deltas provenant de martine
;all #bb06
call xvbl
call next_delta
jp mainloop
;--- routine de deltapacking --------------------------
next_delta:
table_index:
ld a,-1
inc a
cp nbdelta
jr c, table_next
xor a
table_next:
ld (table_index+1),a
add a
ld e,a
ld d,0
ld hl,table_delta
add hl,de
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
delta
ld c,(hl) ; nombre de frame
inc hl
ld b,(hl)
ld (nbdeltas),bc
init
inc hl
ld a,(hl) ; octet a poker
ld (pixel),a
inc hl
ld a,(hl) ;
ld (highbyte_value+1),a ; valeur du HighByte
inc hl
ld c,(hl) ; nbfois
inc hl
ld b,(hl)
ld (nblb), bc ; nombre de LowByte
iter_lowbytes
;
inc hl
ld e,(hl) ; recuperation du lowbyte
highbyte_value ld d,0
ld a,(pixel)
push hl ; on ajoute l'adresse ecran
ld hl,#c000
add hl,de
ld d,h
ld e,l
pop hl
ld (de),a ; poke a l'adresse dans de
ld bc,(nblb)
dec bc
ld (nblb),bc
ld a,b ; test a t'on poke toutes les lowbytes
or a
jr nz, iter_lowbytes
ld a,c
or a
jr nz, iter_lowbytes
ld bc,(nbdeltas)
dec bc
ld (nbdeltas),bc
ld a,b
or a
jr nz,init
ld a,c
or a
jr nz, init
; a t'on encore des frames a traite
ret
;---------------------------------------------------------------
;
; attente de plusieurs vbl
;
xvbl ld e,50
call waitvbl
dec e
jr nz,xvbl+2
ret
;-----------------------------------
;---- attente vbl ----------
waitvbl
ld b,#f5 ; attente vbl
vbl
in a,(c)
rra
jp nc,vbl
ret
;---------------------------
;--- application palette firmware -------------
palettefirmware ; hl pointe sur les valeurs de la palette
ld e,nbcolors
ld a,0
ld hl,palette
paletteloop
ld b,(hl)
ld c,b
push af
push de
push hl
call #bc32 ; af, de, hl corrupted
pop hl
pop de
pop af
inc a
inc hl
dec e
jr nz,paletteloop
ret
;---------------------------------------------
;---------------------------------------------
;---- recuperation de l'adresse de la ligne en dessous ------------
bc26
ld a,h
add a,8
ld h,a ; <---- le fameux que tu as oublié !
ret nc
ld bc,linewidth ; on passe en 96 colonnes
add hl,bc
res 3,h
ret
;-----------------------------------------------------------------
;--- variables memoires -----
pixel db 0
nblb dw 0
nbdeltas dw 0
;----------------------------
{{ .DisplayCode }}
table_delta
{{ .TableDelta }}
Palette:
{{ .DisplayPalette }}
`
var deltaScreenCompressCodeDeltaPlus = `
;--- dimensions du sprite ----
loadingaddress equ #200
linewidth equ {{ .LigneLarge }}
nbdelta equ {{ len .Delta }}
nbcolors equ {{ len .Palette }}
;-----------------------------
org loadingaddress
run loadingaddress
;-----------------------------
start
;--- selection du mode ---------
ld a,{{ .Mode }}
call #BC0E
;--- gestion de la palette ----
DI
LD BC,#BC11
LD HL,UnlockAsic
Unlock:
LD A,(HL)
OUT (C),A
INC HL
DEC C
JR NZ,Unlock
LD BC,#7FB8
OUT (C),C
LD A,(palette)
OUT (C),A
LD HL,palette+1
LD DE,#6400
LD BC,{{ len .Palette }}+{{ len .Palette }}
LDIR
LD BC,#7FA0
OUT (C),C
EI
;------------------------------
jp display_screen
UnlockAsic:
DB #FF,#00,#FF,#77,#B3,#51,#A8,#D4,#62,#39,#9C,#46,#2B,#15,#8A,#CD,#EE
;---------------------------------------------
Palette:
{{ .DisplayPalette }}
display_screen:
LD HL,#b7f9
call #bcdd
call xvbl
;--- affichage du sprite initiale --
; affichage du premier ecran
ld de,#C000
ld hl,sprite
call Depack
;------------------------------------
mainloop ; routine pour afficher les deltas provenant de martine
call xvbl
call next_delta
jp mainloop
;--- routine de deltapacking --------------------------
next_delta:
table_index:
ld a,-1
inc a
cp nbdelta
jr c, table_next
xor a
table_next:
ld (table_index+1),a
add a
ld e,a
ld d,0
ld hl,table_delta
add hl,de
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
ld de,buffer
call Depack
ld hl,buffer ; utilisation de la structure delta décompactée
delta:
ld a,(hl) ; nombre de byte a poker
push af ; stockage en mémoire
inc hl
init
ld a,(hl) ; octet a poker
ld (pixel),a
inc hl
ld c,(hl) ; nbfois
inc hl
ld b,(hl)
inc hl
;
poke_octet
ld e,(hl)
inc hl
ld d,(hl) ; de=adresse
inc hl
ld a,(pixel)
push hl ; on ajoute l'adresse ecran
ld hl,#c000
add hl,de
ld d,h
ld e,l
pop hl
ld (de),a ; poke a l'adresse dans de
dec bc
ld a,b ; test a t'on poke toutes les adresses compteur bc
or a
jr nz, poke_octet
ld a,c
or a
jr nz, poke_octet
pop af
; reste t'il d'autres bytes a poker ?
dec a
push af
jr nz,init
pop af
ret
;
; Decompactage ZX0
; HL = source
; DE = destination
;
Depack:
ld bc,#ffff ; preserve default offset 1
push bc
inc bc
ld a,#80
dzx0s_literals:
call dzx0s_elias ; obtain length
ldir ; copy literals
add a,a ; copy from last offset or new offset?
jr c,dzx0s_new_offset
call dzx0s_elias ; obtain length
dzx0s_copy:
ex (sp),hl ; preserve source,restore offset
push hl ; preserve offset
add hl,de ; calculate destination - offset
ldir ; copy from offset
pop hl ; restore offset
ex (sp),hl ; preserve offset,restore source
add a,a ; copy from literals or new offset?
jr nc,dzx0s_literals
dzx0s_new_offset:
call dzx0s_elias ; obtain offset MSB
ld b,a
pop af ; discard last offset
xor a ; adjust for negative offset
sub c
RET Z ; Plus d'octets a traiter = fini
ld c,a
ld a,b
ld b,c
ld c,(hl) ; obtain offset LSB
inc hl
rr b ; last offset bit becomes first length bit
rr c
push bc ; preserve new offset
ld bc,1 ; obtain length
call nc,dzx0s_elias_backtrack
inc bc
jr dzx0s_copy
dzx0s_elias:
inc c ; interlaced Elias gamma coding
dzx0s_elias_loop:
add a,a
jr nz,dzx0s_elias_skip
ld a,(hl) ; load another group of 8 bits
inc hl
rla
dzx0s_elias_skip:
ret c
dzx0s_elias_backtrack:
add a,a
rl c
rl b
jr dzx0s_elias_loop
ret
;---------------------------------------------------------------
;
; attente de plusieurs vbl
;
xvbl ld e,50
call waitvbl
dec e
jr nz,xvbl+2
ret
;-----------------------------------
;---- attente vbl ----------
waitvbl
ld b,#f5 ; attente vbl
vbl
in a,(c)
rra
jp nc,vbl
ret
;---------------------------
;---- recuperation de l'adresse de la ligne en dessous ------------
bc26
ld a,h
add a,8
ld h,a ; <---- le fameux que tu as oublié !
ret nc
ld bc,linewidth ; on passe en 96 colonnes
add hl,bc
res 3,h
ret
;-----------------------------------------------------------------
;--- variables memoires -----
pixel db 0
nblb dw 0
nbdeltas dw 0
;----------------------------
{{ .DisplayCode }}
table_delta
{{ .TableDelta }}
`
var deltaScreenCodeDeltaPlus = `
;--- dimensions du sprite ----
loadingaddress equ #200
linewidth equ {{ .LigneLarge }}
nbdelta equ {{ len .Delta }}
nbcolors equ {{ len .Palette }}
;-----------------------------
org loadingaddress
run loadingaddress
;-----------------------------
start
;--- selection du mode ---------
ld a,{{ .Mode }}
call #BC0E
;--- gestion de la palette / unlock asic ----
DI
LD BC,#BC11
LD HL,UnlockAsic
Unlock:
LD A,(HL)
OUT (C),A
INC HL
DEC C
JR NZ,Unlock
LD BC,#7FB8
OUT (C),C
LD A,(palette)
OUT (C),A
LD HL,palette+1
LD DE,#6400
LD BC,{{ len .Palette }}+{{ len .Palette }}
LDIR
LD BC,#7FA0
OUT (C),C
EI
;------------------------------
jp display_screen
UnlockAsic:
DB #FF,#00,#FF,#77,#B3,#51,#A8,#D4,#62,#39,#9C,#46,#2B,#15,#8A,#CD,#EE
Palette:
{{ .DisplayPalette }}
display_screen:
call xvbl
;--- affichage du sprite initiale --
; affichage du premier ecran
ld de,#C000
ld hl,sprite
ldir
;------------------------------------
mainloop ; routine pour afficher les deltas provenant de martine
call xvbl
call next_delta
jp mainloop
;--- routine de deltapacking --------------------------
next_delta:
table_index:
ld a,-1
inc a
cp nbdelta
jr c, table_next
xor a
table_next:
ld (table_index+1),a
add a
ld e,a
ld d,0
ld hl,table_delta
add hl,de
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
delta
ld a,(hl) ; nombre de byte a poker
push af ; stockage en mémoire
inc hl
init
ld a,(hl) ; octet a poker
ld (pixel),a
inc hl
ld c,(hl) ; nbfois
inc hl
ld b,(hl)
inc hl
;
poke_octet
ld e,(hl)
inc hl
ld d,(hl) ; de=adresse
inc hl
ld a,(pixel)
push hl ; on ajoute l'adresse ecran
ld hl,#c000
add hl,de
ld d,h
ld e,l
pop hl
ld (de),a ; poke a l'adresse dans de
dec bc
ld a,b ; test a t'on poke toutes les adresses compteur bc
or a
jr nz, poke_octet
ld a,c
or a
jr nz, poke_octet
pop af
; reste t'il d'autres bytes a poker ?
dec a
push af
jr nz,init
pop af