Mercurial > hg > forks > multipaint-js
comparison exporters.pde @ 127:4cebbee4b29b
More work on source export.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 12 Jul 2018 22:33:07 +0300 |
parents | d2b8166fa2a5 |
children | 146879c896b9 |
comparison
equal
deleted
inserted
replaced
126:d2b8166fa2a5 | 127:4cebbee4b29b |
---|---|
28 m_indent = 4; | 28 m_indent = 4; |
29 | 29 |
30 m_byteDef = ".byte"; | 30 m_byteDef = ".byte"; |
31 m_src = ""; | 31 m_src = ""; |
32 m_eol = "\n"; | 32 m_eol = "\n"; |
33 | |
34 m_intmode = 0; | |
33 m_prefix = ""; | 35 m_prefix = ""; |
34 m_intmode = 1; | |
35 } | 36 } |
36 | 37 |
37 void setOffs(int offs) | 38 void setOffs(int offs) |
38 { | 39 { |
39 m_offs = offs; | 40 m_offs = offs; |
62 byte[] getSource() | 63 byte[] getSource() |
63 { | 64 { |
64 return m_src; | 65 return m_src; |
65 } | 66 } |
66 | 67 |
68 String getHexValue(int dval, int dlen) | |
69 { | |
70 static final String mpHexChars[16] = "0123456789abcdef"; | |
71 char[] dstr = new char[dlen]; | |
72 | |
73 for (i = dlen - 1; i >= 0; i--) | |
74 { | |
75 dstr[i] = mpHexChars[dval & 0x0f]; | |
76 dval >>= 4; | |
77 } | |
78 | |
79 return new String(dstr.join('')); | |
80 } | |
81 | |
67 String getByteStr(int val) | 82 String getByteStr(int val) |
68 { | 83 { |
69 switch (m_intmode) | 84 switch (m_intmode) |
70 { | 85 { |
71 case 0: return str(val & 0xff); | 86 case 0: return str(val & 0xff); |
72 } | 87 case 1: return "$"+ getHexValue(val & 0xff, 2); |
73 } | 88 case 2: return getHexValue(val & 0xff, 2) +"h"; |
74 | 89 } |
75 boolean changeState(int nstate) | |
76 { | |
77 boolean changed = (nstate != m_state); | |
78 if (changed && m_state != -1) | |
79 { | |
80 m_src += m_eol; | |
81 } | |
82 | |
83 m_state = nstate; | |
84 return changed; | |
85 } | |
86 | |
87 void addEOL() | |
88 { | |
89 changeState(4); | |
90 m_src += m_eol; | |
91 } | |
92 | |
93 void addByteSep() | |
94 { | |
95 m_src += ","; | |
96 } | 90 } |
97 | 91 |
98 void addIndent() | 92 void addIndent() |
99 { | 93 { |
100 for (int n = 0; n < m_indent; n++) | 94 for (int n = 0; n < m_indent; n++) |
101 m_src += " "; | 95 m_src += " "; |
102 } | 96 } |
103 | 97 |
104 void addByteDef(boolean nl) | 98 void addByteSep() |
105 { | 99 { |
106 if (!nl) | 100 m_src += ","; |
101 } | |
102 | |
103 void addEOL() | |
104 { | |
105 m_state = 4; | |
106 m_src += m_eol; | |
107 } | |
108 | |
109 void addByteDef(boolean force) | |
110 { | |
111 if (m_state != 2 || force) | |
107 { | 112 { |
108 m_src += m_eol; | 113 m_src += m_eol; |
109 addIndent(); | 114 addIndent(); |
110 } | 115 } |
111 | 116 |
114 m_state = 2; | 119 m_state = 2; |
115 } | 120 } |
116 | 121 |
117 void addLabel(String blabel, boolean nl) | 122 void addLabel(String blabel, boolean nl) |
118 { | 123 { |
119 changeState(3); | 124 if (m_state != 3) |
125 addEOL(); | |
126 | |
120 m_src += m_prefix + blabel +":"+ (nl ? " " : ""); | 127 m_src += m_prefix + blabel +":"+ (nl ? " " : ""); |
128 m_state = 3; | |
121 } | 129 } |
122 | 130 |
123 void addComment(String bstr) | 131 void addComment(String bstr) |
124 { | 132 { |
125 changeState(4); | 133 if (m_state != 4) |
134 addEOL(); | |
135 | |
126 m_src += "; "+ bstr + m_eol; | 136 m_src += "; "+ bstr + m_eol; |
137 m_state = 4; | |
127 } | 138 } |
128 | 139 |
129 void addLine(String bstr) | 140 void addLine(String bstr) |
130 { | 141 { |
131 changeState(4); | 142 if (m_state != 4) |
132 m_src += bstr + m_eol; | 143 addEOL(); |
144 | |
145 m_src += bstr.replace("@@", m_prefix) + m_eol; | |
146 m_state = 4; | |
133 } | 147 } |
134 | 148 |
135 void writeByte(int bval, String blabel) | 149 void writeByte(int bval, String blabel) |
136 { | 150 { |
137 m_data[m_offs++] = byte(bval); | 151 m_data[m_offs++] = byte(bval); |
138 | 152 |
139 if (blabel != null) | 153 if (blabel != null) |
140 { | 154 { |
141 addLabel(blabel, true); | 155 addLabel(blabel, true); |
142 addByteDef(true); | 156 m_state = 2; |
157 addByteDef(false); | |
143 m_src += getByteStr(bval); | 158 m_src += getByteStr(bval); |
144 m_src += m_eol; | 159 m_src += m_eol; |
145 } | 160 } |
146 else | 161 else |
147 { | 162 { |
148 if (changeState(2)) | 163 if (m_state != 2) |
149 addByteDef(true); | 164 addByteDef(true); |
150 else | 165 else |
151 if (m_bpl > 0 && m_bytes > m_bpl) | 166 if (m_bpl > 0 && m_bytes > m_bpl) |
152 addByteDef(false); | 167 addByteDef(true); |
153 | 168 |
154 if (m_bytes++ > 0) | 169 if (m_bytes++ > 0) |
155 addByteSep(); | 170 addByteSep(); |
156 | 171 |
157 m_src += getByteStr(bval); | 172 m_src += getByteStr(bval); |
596 } | 611 } |
597 else | 612 else |
598 if (machine == PLUS4M) { //PLUS4 MULTICOLOR | 613 if (machine == PLUS4M) { //PLUS4 MULTICOLOR |
599 ctx.loadTemplate("showpfourm.prg"); | 614 ctx.loadTemplate("showpfourm.prg"); |
600 | 615 |
601 ctx.addLine(".global _bitmap"); | 616 ctx.addLine(".global @@_bitmap"); |
602 ctx.addLine(".global _color1"); | 617 ctx.addLine(".global @@_color1"); |
603 ctx.addLine(".global _color2"); | 618 ctx.addLine(".global @@_color2"); |
604 ctx.addLine(".global _border"); | 619 ctx.addLine(".global @@_border"); |
605 ctx.addLine(".global _back1"); | 620 ctx.addLine(".global @@_back1"); |
606 ctx.addLine(".global _back2"); | 621 ctx.addLine(".global @@_back2"); |
607 | 622 |
608 ctx.addLabel("_bitmap"); | 623 ctx.addLabel("_bitmap"); |
609 ctx.setOffs(0x013e); | 624 ctx.setOffs(0x013e); |
610 mpExportBitmapData(ctx, 40, 25); | 625 mpExportBitmapData(ctx, 40, 25); |
611 | 626 |
648 } | 663 } |
649 else | 664 else |
650 if (machine == PLUS4) { // Plus 4 hires | 665 if (machine == PLUS4) { // Plus 4 hires |
651 ctx.loadTemplate("showpfour.prg"); | 666 ctx.loadTemplate("showpfour.prg"); |
652 | 667 |
653 ctx.addLine(".global _bitmap"); | 668 ctx.addLine(".global @@_bitmap"); |
654 ctx.addLine(".global _color"); | 669 ctx.addLine(".global @@_color"); |
655 ctx.addLine(".global _lumi"); | 670 ctx.addLine(".global @@_lumi"); |
656 ctx.addLine(".global _border"); | 671 ctx.addLine(".global @@_border"); |
657 | 672 |
658 ctx.setOffs(0x0137); | 673 ctx.setOffs(0x0137); |
674 ctx.addLabel("_bitmap"); | |
659 mpExportBitmapData(ctx, 40, 25); | 675 mpExportBitmapData(ctx, 40, 25); |
660 | 676 |
661 val1 = getpluscolor(int(g_map[0])); | 677 val1 = getpluscolor(int(g_map[0])); |
662 val2 = getplusluminance(int(g_map[0])); | 678 val2 = getplusluminance(int(g_map[0])); |
663 val3 = val2 * 16 + val1; | 679 val3 = val2 * 16 + val1; |
664 ctx.setOffs(0x2077); | 680 ctx.writeByteAt(0x2077, val3, "_border"); //border |
665 ctx.writeByte(val3); //border | |
666 | 681 |
667 ctx.setOffs(0x2078); | 682 ctx.setOffs(0x2078); |
683 ctx.addLabel("_color"); | |
668 mpExportColorData(ctx, 40, 25, 65536, 4); //colors | 684 mpExportColorData(ctx, 40, 25, 65536, 4); //colors |
669 | 685 |
670 ctx.setOffs(0x2460); | 686 ctx.setOffs(0x2460); |
687 ctx.addLabel("_lumi"); | |
671 mpExportColorData(ctx, 40, 25, 65536, 5); //lumis | 688 mpExportColorData(ctx, 40, 25, 65536, 5); //lumis |
672 | 689 |
673 //plus4 hires | 690 //plus4 hires |
674 //0x0137 bitmap (40 x 25 x 8) | 691 //0x0137 bitmap (40 x 25 x 8) |
675 //0x2077 borderi | 692 //0x2077 borderi |
678 } | 695 } |
679 else | 696 else |
680 if (machine == MSX) { // MSX | 697 if (machine == MSX) { // MSX |
681 ctx.loadTemplate("msxshow.com"); | 698 ctx.loadTemplate("msxshow.com"); |
682 | 699 |
683 ctx.addLine(".globl _bitmap,_nimi2"); | 700 ctx.addLine(".globl @@_bitmap,@@_nimi2"); |
684 ctx.addLine(".area _CODE"); | 701 ctx.addLine(".area _CODE"); |
685 | 702 |
686 ctx.setOffs(0x00f9); | 703 ctx.setOffs(0x00f9); |
687 ctx.addLabel("_bitmap"); | 704 ctx.addLabel("_bitmap"); |
688 mpExportBitmapData(ctx, 32, 24); | 705 mpExportBitmapData(ctx, 32, 24); |
703 if (machine == SPECTRUM) { // ZX Spectrum | 720 if (machine == SPECTRUM) { // ZX Spectrum |
704 | 721 |
705 //would need some cleaning up | 722 //would need some cleaning up |
706 | 723 |
707 // ctx.addLine(".area _DATA"); | 724 // ctx.addLine(".area _DATA"); |
708 // ctx.addLine(".globl _taustakuva"); | 725 // ctx.addLine(".globl @@_taustakuva"); |
709 // ctx.addLabel("_taustakuva"); | 726 // ctx.addLabel("_taustakuva"); |
710 ctx.loadTemplate("specshow.tap"); | 727 ctx.loadTemplate("specshow.tap"); |
711 | 728 |
712 int checksum = 0xc9; | 729 int checksum = 0xc9; |
713 for (y = 0x1d24; y <= 0x1d3d; y++) { //1d3e | 730 for (y = 0x1d24; y < 0x1d3e; y++) |
731 { | |
714 checksum = checksum ^ int(g_template[y]); | 732 checksum = checksum ^ int(g_template[y]); |
715 } | 733 } |
716 | 734 |
717 //TAP requires fiddling with the checksum | 735 //TAP requires fiddling with the checksum |
718 //println("Checksum:"+hex(checksum,2)); | 736 //println("Checksum:"+hex(checksum,2)); |
719 ctx.addLabel("paske"); | 737 ctx.writeByteAt(0x0223, g_map[0], "_something1"); |
720 ctx.writeByteAt(0x0223, g_map[0]); | |
721 checksum = checksum ^ int(g_map[0]); | 738 checksum = checksum ^ int(g_map[0]); |
722 | 739 |
723 ctx.addLabel("jottain"); | 740 ctx.addLabel("_something2"); |
741 ctx.setBPL(32); | |
724 for (int y = 0; y <= 2; y++) | 742 for (int y = 0; y <= 2; y++) |
725 for (int y2 = 0; y2 < 8; y2++) | 743 for (int y2 = 0; y2 < 8; y2++) |
726 for (int yy = 0; yy < 8; yy++) | 744 for (int yy = 0; yy < 8; yy++) |
727 { | 745 for (int x = 0; x < 32; x++) |
728 for (int x = 0; x < 32; x++) | 746 { |
747 int yp = y * 64 + yy * 8 + y2, | |
748 ad = 1024 + yp * 256 + x * 8, | |
749 valu = | |
750 g_map[ad + 0] * 128 + | |
751 g_map[ad + 1] * 64 + | |
752 g_map[ad + 2] * 32 + | |
753 g_map[ad + 3] * 16 + | |
754 g_map[ad + 4] * 8 + | |
755 g_map[ad + 5] * 4 + | |
756 g_map[ad + 6] * 2 + | |
757 g_map[ad + 7] * 1; | |
758 | |
759 ctx.writeByte(valu); | |
760 checksum = checksum ^ int(valu); | |
761 } | |
762 | |
763 ctx.addLabel("_attributes"); | |
764 for (int y = 0; y < 24; y++) | |
765 for (int x = 0; x < 32; x++) | |
766 { | |
767 int ad = 65536 + x + y * 256, | |
768 valu = int(g_map[ad]), | |
769 valu2 = int(g_map[ad + MX * MY * 8]), | |
770 bri = 0; | |
771 | |
772 if (valu >= 8) | |
729 { | 773 { |
730 int yp = y * 64 + yy * 8 + y2, | 774 bri = 1; |
731 ad = 1024 + yp * 256 + x * 8, | 775 valu = valu - 8; |
732 valu = | 776 valu2 = valu2 - 8; |
733 g_map[ad + 0] * 128 + | 777 if (valu2 <= 0) { |
734 g_map[ad + 1] * 64 + | 778 valu2 = 0; |
735 g_map[ad + 2] * 32 + | 779 } |
736 g_map[ad + 3] * 16 + | |
737 g_map[ad + 4] * 8 + | |
738 g_map[ad + 5] * 4 + | |
739 g_map[ad + 6] * 2 + | |
740 g_map[ad + 7] * 1; | |
741 | |
742 ctx.writeByte(valu); | |
743 checksum = checksum ^ int(valu); | |
744 } | 780 } |
745 } | 781 if (bri == 1) { |
746 | 782 valu = valu + 64; |
747 ctx.addLabel("attributes"); | |
748 for (int y = 0; y < 24; y++) | |
749 { | |
750 for (int x = 0; x < 32; x++) | |
751 { | |
752 int ad = 65536 + x + y * 256, | |
753 valu = int(g_map[ad]), | |
754 valu2 = int(g_map[ad + MX * MY * 8]), | |
755 bri = 0; | |
756 | |
757 if (valu >= 8) | |
758 { | |
759 bri = 1; | |
760 valu = valu - 8; | |
761 valu2 = valu2 - 8; | |
762 if (valu2 <= 0) { | |
763 valu2 = 0; | |
764 } | |
765 } | |
766 if (bri == 1) { | |
767 valu = valu + 64; | |
768 } | |
769 | |
770 ctx.writeByte(valu + valu2 * 8); | |
771 checksum = checksum ^ int(valu + valu2 * 8); | |
772 } | 783 } |
773 } | 784 |
774 | 785 ctx.writeByte(valu + valu2 * 8); |
775 //println("checksum:"+hex(checksum,2)); | 786 checksum = checksum ^ int(valu + valu2 * 8); |
776 ctx.writeByteAt(0x1d3e, int(checksum)); | 787 } |
788 ctx.setBPLOff(); | |
789 | |
790 ctx.writeByteAt(0x1d3e, int(checksum), "_checksum"); | |
777 } | 791 } |
778 else | 792 else |
779 if (machine == CPC) { | 793 if (machine == CPC) { |
780 int val1, val2, val3, valu; | 794 int val1, val2, val3, valu; |
781 ctx.loadTemplate("cpc-mode0.bin"); | 795 ctx.loadTemplate("cpc-mode0.bin"); |
784 ctx.addLabel("_bitmap"); | 798 ctx.addLabel("_bitmap"); |
785 mpExportBitmapData_CPC(ctx, 160, 200); | 799 mpExportBitmapData_CPC(ctx, 160, 200); |
786 | 800 |
787 //fixed # of palette entries, just trying to be generic | 801 //fixed # of palette entries, just trying to be generic |
788 ctx.setOffs(16453); | 802 ctx.setOffs(16453); |
789 ctx.setBPL(g_maxcolors - 1); | 803 ctx.setBPL(32); |
790 ctx.addLabel("_palette"); | 804 ctx.addLabel("_palette"); |
791 for (int i = 0; i < g_maxcolors; i++) | 805 for (int i = 0; i < g_maxcolors; i++) |
792 { | 806 { |
793 val1 = int(g_g[i] / (256 / g_palsteps)); | 807 val1 = int(g_g[i] / (256 / g_palsteps)); |
794 val2 = int(g_r[i] / (256 / g_palsteps)); | 808 val2 = int(g_r[i] / (256 / g_palsteps)); |
795 val3 = int(g_b[i] / (256 / g_palsteps)); | 809 val3 = int(g_b[i] / (256 / g_palsteps)); |
796 | 810 |
797 ctx.writeByte(ctx, val1 * 9 + val2 * 3 + val3); | 811 ctx.writeByte(val1 * 9 + val2 * 3 + val3); |
798 } | 812 } |
799 ctx.setBPLOff(); | 813 ctx.setBPLOff(); |
800 | 814 |
801 valu = int(g_map[0]); | 815 valu = int(g_map[0]); |
802 val1 = int(g_g[valu] / (256 / g_palsteps)); | 816 val1 = int(g_g[valu] / (256 / g_palsteps)); |
813 //16469 border color (0..26) | 827 //16469 border color (0..26) |
814 } | 828 } |
815 else | 829 else |
816 return false; | 830 return false; |
817 | 831 |
832 ctx.addEOL(); | |
818 return true; | 833 return true; |
819 } | 834 } |
820 | 835 |
821 | 836 |
822 void mpExportBitmapData_CPC(MPWriteCtx ctx, int xwid, int yy) | 837 void mpExportBitmapData_CPC(MPWriteCtx ctx, int xwid, int yy) |
823 { | 838 { |
824 int x, y, ad, val1, val2, y2; | 839 int x, y, ad, val1, val2, y2; |
825 int pix0b0, pix0b1, pix0b2, pix0b3; | 840 int pix0b0, pix0b1, pix0b2, pix0b3; |
826 int pix1b0, pix1b1, pix1b2, pix1b3; | 841 int pix1b0, pix1b1, pix1b2, pix1b3; |
827 | 842 |
828 ctx.setBPL(xwid / 2); | 843 ctx.setBPL(32); |
829 | 844 |
830 for (y2 = 0; y2 < 8; y2++) | 845 for (y2 = 0; y2 < 8; y2++) |
831 { | 846 { |
832 ctx.addByteDef(); | 847 ctx.addByteDef(true); |
833 | 848 |
834 for (y = 0; y < 25; y++) | 849 for (y = 0; y < 25; y++) |
835 for (x = 0; x < int(xwid / 2); x++) | 850 for (x = 0; x < int(xwid / 2); x++) |
836 { | 851 { |
837 ad = 1024 + (y * 8) * X + (y2 * X) + x * 4; | 852 ad = 1024 + (y * 8) * X + (y2 * X) + x * 4; |
860 val2 = pix1b3 + pix0b3 * 2 + pix1b1 * 4 + pix0b1 * 8 + pix1b2 * 16 + pix0b2 * 32 + pix1b0 * 64 + pix0b0 * 128; | 875 val2 = pix1b3 + pix0b3 * 2 + pix1b1 * 4 + pix0b1 * 8 + pix1b2 * 16 + pix0b2 * 32 + pix1b0 * 64 + pix0b0 * 128; |
861 | 876 |
862 ctx.writeByte(val2); | 877 ctx.writeByte(val2); |
863 } | 878 } |
864 | 879 |
865 ctx.addByteDef(); | 880 ctx.addByteDef(true); |
866 for (int j = 0; j <= 47; j++) | 881 for (int j = 0; j <= 47; j++) |
867 ctx.writeByte(0); | 882 ctx.writeByte(0); |
868 } | 883 } |
869 ctx.setBPLOff(); | 884 ctx.setBPLOff(); |
870 ctx.addEOL(); | 885 ctx.addEOL(); |
876 int linep = 0, | 891 int linep = 0, |
877 xwid = xx * 8, | 892 xwid = xx * 8, |
878 ywid = yy * 8; | 893 ywid = yy * 8; |
879 | 894 |
880 ctx.setBPL(32); | 895 ctx.setBPL(32); |
881 ctx.addByteDef(); | 896 ctx.addByteDef(true); |
882 | 897 |
883 for (int y = 0; y < yy; y++) | 898 for (int y = 0; y < yy; y++) |
884 for (int x = 0; x < xx; x++) | 899 for (int x = 0; x < xx; x++) |
885 { | 900 { |
886 for (int y2 = 0; y2 < 8; y2++) | 901 for (int y2 = 0; y2 < 8; y2++) |
967 myrpsi = 7; | 982 myrpsi = 7; |
968 } | 983 } |
969 | 984 |
970 for (int yc = 0; yc < height; yc++) | 985 for (int yc = 0; yc < height; yc++) |
971 { | 986 { |
972 ctx.addByteDef(); | 987 ctx.addByteDef(true); |
973 for (int xc = 0; xc < width; xc++) | 988 for (int xc = 0; xc < width; xc++) |
974 for (int yline = 0; yline <= myrpsi; yline++) | 989 for (int yline = 0; yline <= myrpsi; yline++) |
975 { | 990 { |
976 int valu1, valu2, ad = srcoffs + xc + yc * xmult + yline * width; | 991 int valu1, valu2, ad = srcoffs + xc + yc * xmult + yline * width; |
977 | 992 |