Previous 15994 Revisions Next

r13617 Sunday 18th December, 2011 at 14:46:45 UTC by Wilbert Pol
sms.c: Moved banking to separate methods reducing the amount of copy/pasted code. (nw)
[src/mess/drivers]sms.c
[src/mess/includes]sms.h
[src/mess/machine]sms.c

src/mess/machine/sms.c
r13616r13617
99#define VERBOSE 0
1010#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
1111
12#define CF_CODEMASTERS_MAPPER    0x01
13#define CF_KOREAN_MAPPER         0x02
14#define CF_KOREAN_ZEMINA_MAPPER  0x04
15#define CF_KOREAN_NOBANK_MAPPER  0x08
16#define CF_93C46_EEPROM          0x10
17#define CF_ONCART_RAM            0x20
18#define CF_GG_SMS_MODE           0x40
12#define CF_CODEMASTERS_MAPPER      0x001
13#define CF_KOREAN_MAPPER           0x002
14#define CF_KOREAN_ZEMINA_MAPPER    0x004
15#define CF_KOREAN_NOBANK_MAPPER    0x008
16#define CF_93C46_EEPROM            0x010
17#define CF_ONCART_RAM              0x020
18#define CF_GG_SMS_MODE             0x040
19#define CF_KOREAN_ZEMINA_NEMESIS   0x080
20#define CF_4PAK_MAPPER             0x100
21#define CF_JANGGUN_MAPPER          0x200
22#define CF_TVDRAW                  0x400
1923
2024#define LGUN_RADIUS           6
2125#define LGUN_X_INTERVAL       4
r13616r13617
3236}
3337
3438
39void sms_state::map_cart_16k( UINT16 address, UINT16 bank )
40{
41   switch ( address )
42   {
43   case 0x0000:
44      map_cart_8k( 0x0000, bank * 2 );
45      map_cart_8k( 0x2000, bank * 2 + 1 );
46      break;
47
48   case 0x0400:
49      map_cart_8k( 0x0400, bank * 2 );
50      map_cart_8k( 0x2000, bank * 2 + 1 );
51      break;
52
53   case 0x4000:
54      map_cart_8k( 0x4000, bank * 2 );
55      map_cart_8k( 0x6000, bank * 2 + 1 );
56      break;
57
58   case 0x8000:
59      map_cart_8k( 0x8000, bank * 2 );
60      map_cart_8k( 0xA000, bank * 2 + 1 );
61      break;
62
63   default:
64      fatalerror("map_cart_16k: Unsupported map address %04x passed\n", address);
65      break;
66   }
67}
68
69
70void sms_state::map_cart_8k( UINT16 address, UINT16 bank )
71{
72   UINT8 *bank_start = m_banking_none;
73
74   if ( m_cartridge[m_current_cartridge].ROM )
75   {
76      UINT8 rom_bank_count = m_cartridge[m_current_cartridge].size / 0x2000;
77      bank_start = m_cartridge[m_current_cartridge].ROM + ((rom_bank_count > 0) ? bank % rom_bank_count : 0) * 0x2000;
78   }
79
80   switch ( address )
81   {
82   case 0x0000:
83      m_banking_cart[1] = bank_start;
84      m_banking_cart[2] = bank_start + 0x400;
85      memory_set_bankptr(machine(), "bank1", m_banking_cart[1]);
86      memory_set_bankptr(machine(), "bank2", m_banking_cart[2]);
87      break;
88
89   case 0x0400:
90      m_banking_cart[2] = bank_start + 0x400;
91      memory_set_bankptr(machine(), "bank2", m_banking_cart[2]);
92      break;
93
94   case 0x2000:
95      m_banking_cart[7] = bank_start;
96      memory_set_bankptr(machine(), "bank7", m_banking_cart[7]);
97      break;
98
99   case 0x4000:
100      m_banking_cart[3] = bank_start;
101      memory_set_bankptr(machine(), "bank3", m_banking_cart[3]);
102      break;
103
104   case 0x6000:
105      m_banking_cart[4] = bank_start;
106      memory_set_bankptr(machine(), "bank4", m_banking_cart[4]);
107      break;
108
109   case 0x8000:
110      m_banking_cart[5] = bank_start;
111      memory_set_bankptr(machine(), "bank5", m_banking_cart[5]);
112      break;
113
114   case 0xA000:
115      m_banking_cart[6] = bank_start;
116      memory_set_bankptr(machine(), "bank6", m_banking_cart[6]);
117      break;
118
119   default:
120      fatalerror("map_cart_8k: Unsuppored map address %04x passed\n", address);
121      break;
122   }
123}
124
125
126void sms_state::map_bios_16k( UINT16 address, UINT16 bank)
127{
128   switch ( address )
129   {
130   case 0x0000:
131      map_bios_8k( 0x0000, bank * 2 );
132      map_bios_8k( 0x2000, bank * 2 + 1 );
133      break;
134
135   case 0x0400:
136      map_bios_8k( 0x0400, bank * 2 );
137      map_bios_8k( 0x2000, bank * 2 + 1 );
138      break;
139
140   case 0x4000:
141      map_bios_8k( 0x4000, bank * 2 );
142      map_bios_8k( 0x6000, bank * 2 + 1 );
143      break;
144
145   case 0x8000:
146      map_bios_8k( 0x8000, bank * 2 );
147      map_bios_8k( 0xA000, bank * 2 + 1 );
148      break;
149
150   default:
151      fatalerror("map_bios_16k: Unsupported map address %04x passed\n", address);
152      break;
153   }
154}
155
156
157void sms_state::map_bios_8k( UINT16 address, UINT16 bank )
158{
159   UINT8 *bank_start = m_banking_none;
160
161   if ( m_BIOS )
162   {
163      bank_start = m_BIOS + ((m_bios_page_count > 0) ? bank % m_bios_page_count : 0) * 0x2000;
164   }
165
166   switch ( address )
167   {
168   case 0x0000:
169      m_banking_bios[1] = bank_start;
170      memory_set_bankptr(machine(), "bank1", m_banking_bios[1]);
171      break;
172
173   case 0x0400:
174      if ( m_has_bios_0400 )
175      {
176         break;
177      }
178      m_banking_bios[2] = bank_start + 0x400;
179      memory_set_bankptr(machine(), "bank2", m_banking_bios[2]);
180      break;
181
182   case 0x2000:
183      if ( m_has_bios_0400 || m_has_bios_2000 )
184      {
185         break;
186      }
187      m_banking_bios[7] = bank_start;
188      memory_set_bankptr(machine(), "bank7", m_banking_bios[7]);
189      break;
190
191   case 0x4000:
192      if ( m_has_bios_0400 || m_has_bios_2000 )
193      {
194         break;
195      }
196      m_banking_bios[3] = bank_start;
197      memory_set_bankptr(machine(), "bank3", m_banking_bios[3]);
198      break;
199
200   case 0x6000:
201      if ( m_has_bios_0400 || m_has_bios_2000 )
202      {
203         break;
204      }
205      m_banking_bios[4] = bank_start;
206      memory_set_bankptr(machine(), "bank4", m_banking_bios[4]);
207      break;
208
209   case 0x8000:
210      if ( m_has_bios_0400 || m_has_bios_2000 )
211      {
212         break;
213      }
214      m_banking_bios[5] = bank_start;
215      memory_set_bankptr(machine(), "bank5", m_banking_bios[5]);
216      break;
217
218   case 0xA000:
219      if ( m_has_bios_0400 || m_has_bios_2000 )
220      {
221         break;
222      }
223      m_banking_bios[6] = bank_start;
224      memory_set_bankptr(machine(), "bank6", m_banking_bios[6]);
225      break;
226
227   default:
228      fatalerror("map_cart_8k: Unsuppored map address %04x passed\n", address);
229      break;
230   }
231}
232
233
35234static WRITE8_HANDLER( sms_input_write )
36235{
37236   sms_state *state = space->machine().driver_data<sms_state>();
r13616r13617
697896
698897   if (data & 0x01)
699898   {
700      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_X") : 0x80;
899      state->m_cartridge[state->m_current_cartridge].m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_X") : 0x80;
701900
702      if (state->m_tvdraw_data < 4) state->m_tvdraw_data = 4;
703      if (state->m_tvdraw_data > 251) state->m_tvdraw_data = 251;
901      if (state->m_cartridge[state->m_current_cartridge].m_tvdraw_data < 4) state->m_cartridge[state->m_current_cartridge].m_tvdraw_data = 4;
902      if (state->m_cartridge[state->m_current_cartridge].m_tvdraw_data > 251) state->m_cartridge[state->m_current_cartridge].m_tvdraw_data = 251;
704903   }
705904   else
706905   {
707      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_Y") + 0x20 : 0x80;
906      state->m_cartridge[state->m_current_cartridge].m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_Y") + 0x20 : 0x80;
708907   }
709908}
710909
r13616r13617
717916static READ8_HANDLER( sms_tvdraw_data_r )
718917{
719918   sms_state *state = space->machine().driver_data<sms_state>();
720   return state->m_tvdraw_data;
919   return state->m_cartridge[state->m_current_cartridge].m_tvdraw_data;
721920}
722921
723922
724923WRITE8_HANDLER( sms_mapper_w )
725924{
726925   sms_state *state = space->machine().driver_data<sms_state>();
727   int page;
728   UINT8 *SOURCE_BIOS;
729   UINT8 *SOURCE_CART;
730   UINT8 *SOURCE;
731   UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
926   bool bios_selected = false;
927   bool cartridge_selected = false;
732928
733929   offset &= 3;
734930
735931   state->m_mapper[offset] = data;
736932   state->m_mapper_ram[offset] = data;
737933
738   if (state->m_cartridge[state->m_current_cartridge].ROM)
739   {
740      SOURCE_CART = state->m_cartridge[state->m_current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
741   }
742   else
743   {
744      SOURCE_CART = state->m_banking_none[1];
745   }
746
747   if (state->m_BIOS)
748   {
749      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
750   }
751   else
752   {
753      SOURCE_BIOS = state->m_banking_none[1];
754   }
755
756934   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
757935   {
758936      if (!(state->m_bios_port & IO_CARTRIDGE) || (state->m_is_gamegear && state->m_BIOS == NULL))
759937      {
760         page = (rom_page_count > 0) ? data % rom_page_count : 0;
761938         if (!state->m_cartridge[state->m_current_cartridge].ROM)
762939            return;
763         SOURCE = SOURCE_CART;
940         cartridge_selected = true;
764941      }
765942      else
766943      {
r13616r13617
770947   }
771948   else
772949   {
773      page = (state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0;
774950      if (!state->m_BIOS)
775951         return;
776      SOURCE = SOURCE_BIOS;
952      bios_selected = true;
777953   }
778954
779955   switch (offset)
r13616r13617
782958      /* Is it ram or rom? */
783959      if (data & 0x08) /* it's ram */
784960      {
961         UINT8 *sram = NULL;
785962         state->m_cartridge[state->m_current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
786963         if (data & 0x04)
787964         {
788            LOG(("ram 1 paged.\n"));
789            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM + 0x4000;
965            sram = state->m_cartridge[state->m_current_cartridge].cartSRAM + 0x4000;
790966         }
791967         else
792968         {
793            LOG(("ram 0 paged.\n"));
794            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM;
969            sram = state->m_cartridge[state->m_current_cartridge].cartSRAM;
795970         }
796         memory_set_bankptr(space->machine(),  "bank5", SOURCE);
797         memory_set_bankptr(space->machine(),  "bank6", SOURCE + 0x2000);
971         memory_set_bankptr(space->machine(),  "bank5", sram);
972         memory_set_bankptr(space->machine(),  "bank6", sram + 0x2000);
798973      }
799974      else /* it's rom */
800975      {
801976         if (state->m_bios_port & IO_BIOS_ROM || ! state->m_has_bios)
802977         {
803            page = (rom_page_count > 0) ? state->m_mapper[3] % rom_page_count : 0;
804            SOURCE = state->m_banking_cart[5];
978            if ( ! state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_NOBANK_MAPPER )
979            {
980               state->map_cart_16k( 0x8000, state->m_mapper[3] );
981            }
805982         }
806983         else
807984         {
808            page = (state->m_bios_page_count > 0) ? state->m_mapper[3] % state->m_bios_page_count : 0;
809            SOURCE = state->m_banking_bios[5];
985            state->map_bios_16k( 0x8000, state->m_mapper[3] );
810986         }
811         LOG(("rom 2 paged in %x.\n", page));
812         memory_set_bankptr(space->machine(), "bank5", SOURCE);
813         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
814987      }
815988      break;
816989
817990   case 1: /* Select 16k ROM bank for 0400-3FFF */
818      LOG(("rom 0 paged in %x.\n", page));
819      state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
820      state->m_banking_cart[2] = SOURCE_CART + 0x0400;
821      if (state->m_is_gamegear)
822         SOURCE = SOURCE_CART;
823
824      memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
991      if ( cartridge_selected || state->m_is_gamegear )
992      {
993         if ( ! state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_NOBANK_MAPPER )
994         {
995            state->map_cart_16k( 0x400, data );
996         }
997      }
998      if ( bios_selected )
999      {
1000         state->map_bios_16k( 0x400, data );
1001      }
8251002      break;
8261003
8271004   case 2: /* Select 16k ROM bank for 4000-7FFF */
828      LOG(("rom 1 paged in %x.\n", page));
829      state->m_banking_bios[3] = SOURCE_BIOS;
830      state->m_banking_cart[3] = SOURCE_CART;
831      if (state->m_is_gamegear)
832         SOURCE = SOURCE_CART;
833
834      memory_set_bankptr(space->machine(), "bank3", SOURCE);
835      memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
1005      if ( cartridge_selected || state->m_is_gamegear )
1006      {
1007         if ( ! state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_NOBANK_MAPPER )
1008         {
1009            state->map_cart_16k( 0x4000, data );
1010         }
1011      }
1012      if ( bios_selected )
1013      {
1014         state->map_bios_16k( 0x4000, data );
1015      }
8361016      break;
8371017
8381018   case 3: /* Select 16k ROM bank for 8000-BFFF */
839      state->m_banking_bios[5] = SOURCE_BIOS;
840      if (state->m_is_gamegear)
841         SOURCE = SOURCE_CART;
842
843      if ( state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1019      if ( cartridge_selected || state->m_is_gamegear )
8441020      {
845         if (SOURCE == SOURCE_CART)
1021         if ( state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
8461022         {
847            SOURCE = state->m_banking_cart[5];
1023            return;
8481024         }
1025
1026         if ( ! ( state->m_mapper[0] & 0x08 ) )      // Is RAM disabled
1027         {
1028            if ( ! state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_NOBANK_MAPPER )
1029            {
1030               state->map_cart_16k( 0x8000, data );
1031            }
1032         }
8491033      }
850      else
851      {
852         state->m_banking_cart[5] = SOURCE_CART;
853      }
8541034
855      if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
1035      if ( bios_selected )
8561036      {
857         LOG(("rom 2 paged in %x.\n", page));
858         memory_set_bankptr(space->machine(), "bank5", SOURCE);
859         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
1037         if ( ! ( state->m_mapper[0] & 0x08 ) )      // Is RAM disabled
1038         {
1039            state->map_bios_16k( 0x8000, data );
1040         }
8601041      }
8611042      break;
8621043   }
r13616r13617
8681049
8691050   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
8701051   {
871      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x2000;
872      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
873
8741052      if (!state->m_cartridge[state->m_current_cartridge].ROM)
8751053         return;
8761054
8771055      switch (offset & 3)
8781056      {
8791057         case 0:
880            state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
881            memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1058            state->map_cart_8k( 0x8000, data );
8821059            break;
8831060         case 1:
884            state->m_banking_cart[6] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
885            memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[6]);
1061            state->map_cart_8k( 0xA000, data );
8861062            break;
8871063         case 2:
888            state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
889            memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
1064            state->map_cart_8k( 0x4000, data );
8901065            break;
8911066         case 3:
892            state->m_banking_cart[4] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
893            memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[4]);
1067            state->map_cart_8k( 0x6000, data );
8941068            break;
8951069      }
896      LOG(("Zemina mapper write: offset %x data %x.\n", offset, page));
1070      LOG(("Zemina mapper write: offset %x data %x.\n", offset, data));
8971071   }
8981072}
8991073
r13616r13617
9031077
9041078   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
9051079   {
906      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
907      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
908      state->m_banking_cart[2] = state->m_banking_cart[1] + 0x0400;
909      memory_set_bankptr(space->machine(), "bank1", state->m_banking_cart[1]);
910      memory_set_bankptr(space->machine(), "bank2", state->m_banking_cart[2]);
1080      state->map_cart_16k( 0x0000, data );
9111081   }
9121082}
9131083
r13616r13617
9261096      }
9271097      else
9281098      {
929         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
930         state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
931         memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
932         memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[3] + 0x2000);
1099         state->map_cart_16k( 0x4000, data );
9331100         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
9341101      }
9351102   }
9361103}
9371104
938static READ8_HANDLER( sms_kor_nobank_r )
1105
1106WRITE8_HANDLER( sms_4pak_page0_w )
9391107{
940//  printf("read %x\n", offset);
941   return space->read_byte(0xdffc + offset);
1108   sms_state *state = space->machine().driver_data<sms_state>();
1109
1110   state->m_cartridge[state->m_current_cartridge].m_4pak_page0 = data;
1111
1112   state->map_cart_16k( 0x0000, data );
1113   state->map_cart_16k( 0x8000, ( state->m_cartridge[state->m_current_cartridge].m_4pak_page0 & 0x30 ) + state->m_cartridge[state->m_current_cartridge].m_4pak_page2 );
9421114}
9431115
944static WRITE8_HANDLER( sms_kor_nobank_w )
1116
1117WRITE8_HANDLER( sms_4pak_page1_w )
9451118{
9461119   sms_state *state = space->machine().driver_data<sms_state>();
947   UINT8 *SOURCE_BIOS;
948   UINT8 *SOURCE = NULL;
9491120
950   space->write_byte(0xdffc + offset, data);
951   state->m_mapper[offset] = data;
1121   state->m_cartridge[state->m_current_cartridge].m_4pak_page1 = data;
9521122
953   // FIXME: the code below (which only handles the BIOS bankswitch at start) should be cleaned up
954   // currently, it's just a stripped down version of sms_mapper_w
955   if (state->m_BIOS)
956      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
957   else
958      SOURCE_BIOS = state->m_banking_none[1];
1123   state->map_cart_16k( 0x4000, data );
1124}
9591125
960   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
961      return;
962   else
963   {
964      if (!state->m_BIOS)
965         return;
966      SOURCE = SOURCE_BIOS;
967   }
9681126
969//  printf("write %x, %x\n", offset, data);
1127WRITE8_HANDLER( sms_4pak_page2_w )
1128{
1129   sms_state *state = space->machine().driver_data<sms_state>();
9701130
971   switch (offset)
972   {
973      case 0: /* Control */
974         if (!(data & 0x08)) /* it's not ram */
975         {
976            if (!(state->m_bios_port & IO_BIOS_ROM) && state->m_has_bios)
977            {
978               SOURCE = state->m_banking_bios[5];
979               memory_set_bankptr(space->machine(), "bank5", SOURCE);
980               memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
981            }
982         }
983         break;
1131   state->m_cartridge[state->m_current_cartridge].m_4pak_page2 = data;
9841132
985      case 1: /* Select 16k ROM bank for 0400-3FFF */
986         state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
987         memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
988         break;
1133   state->map_cart_16k( 0x8000, ( state->m_cartridge[state->m_current_cartridge].m_4pak_page0 & 0x30 ) + state->m_cartridge[state->m_current_cartridge].m_4pak_page2 );
1134}
9891135
990      case 2: /* Select 16k ROM bank for 4000-7FFF */
991         state->m_banking_bios[3] = SOURCE_BIOS;
992         memory_set_bankptr(space->machine(), "bank3", SOURCE);
993         memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
994         break;
9951136
996      case 3: /* Select 16k ROM bank for 8000-BFFF */
997         state->m_banking_bios[5] = SOURCE_BIOS;
998         if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
999         {
1000            memory_set_bankptr(space->machine(), "bank5", SOURCE);
1001            memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
1002         }
1003         break;
1004   }
1137WRITE8_HANDLER( sms_janggun_bank0_w )
1138{
1139   sms_state *state = space->machine().driver_data<sms_state>();
1140
1141   state->map_cart_8k( 0x4000, data );
10051142}
10061143
1144
1145WRITE8_HANDLER( sms_janggun_bank1_w )
1146{
1147   sms_state *state = space->machine().driver_data<sms_state>();
1148
1149   state->map_cart_8k( 0x6000, data );
1150}
1151
1152
1153WRITE8_HANDLER( sms_janggun_bank2_w )
1154{
1155   sms_state *state = space->machine().driver_data<sms_state>();
1156
1157   state->map_cart_8k( 0x8000, data );
1158}
1159
1160
1161WRITE8_HANDLER( sms_janggun_bank3_w )
1162{
1163   sms_state *state = space->machine().driver_data<sms_state>();
1164
1165   state->map_cart_8k( 0xA000, data );
1166}
1167
1168
10071169WRITE8_HANDLER( sms_bios_w )
10081170{
10091171   sms_state *state = space->machine().driver_data<sms_state>();
r13616r13617
10391201
10401202   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
10411203   {
1042      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1043      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
1044
1045      if (!state->m_cartridge[state->m_current_cartridge].ROM)
1046         return;
1047
1048      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1049      memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1050      memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1051      LOG(("rom 2 paged in %x (Korean mapper).\n", page));
1204      state->map_cart_16k( 0x8000, data );
10521205   }
10531206}
10541207
r13616r13617
11651318   running_machine &machine = space->machine();
11661319
11671320   /* 1. set up bank pointers to point to nothing */
1168   memory_set_bankptr(machine, "bank1", state->m_banking_none[1]);
1169   memory_set_bankptr(machine, "bank2", state->m_banking_none[2]);
1170   memory_set_bankptr(machine, "bank3", state->m_banking_none[3]);
1171   memory_set_bankptr(machine, "bank4", state->m_banking_none[3] + 0x2000);
1172   memory_set_bankptr(machine, "bank5", state->m_banking_none[5]);
1173   memory_set_bankptr(machine, "bank6", state->m_banking_none[5] + 0x2000);
1321   memory_set_bankptr(machine, "bank1", state->m_banking_none);
1322   memory_set_bankptr(machine, "bank2", state->m_banking_none);
1323   memory_set_bankptr(machine, "bank7", state->m_banking_none);
1324   memory_set_bankptr(machine, "bank3", state->m_banking_none);
1325   memory_set_bankptr(machine, "bank4", state->m_banking_none);
1326   memory_set_bankptr(machine, "bank5", state->m_banking_none);
1327   memory_set_bankptr(machine, "bank6", state->m_banking_none);
11741328
11751329   /* 2. check and set up expansion port */
11761330   if (!(state->m_bios_port & IO_EXPANSION) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_CARD))
r13616r13617
11931347   {
11941348      memory_set_bankptr(machine, "bank1", state->m_banking_cart[1]);
11951349      memory_set_bankptr(machine, "bank2", state->m_banking_cart[2]);
1350      memory_set_bankptr(machine, "bank7", state->m_banking_cart[7]);
11961351      memory_set_bankptr(machine, "bank3", state->m_banking_cart[3]);
11971352      memory_set_bankptr(machine, "bank4", state->m_banking_cart[3] + 0x2000);
11981353      memory_set_bankptr(machine, "bank5", state->m_banking_cart[5]);
r13616r13617
12201375      {
12211376         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
12221377         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1378         memory_set_bankptr(machine, "bank7", state->m_banking_bios[7]);
12231379         memory_set_bankptr(machine, "bank3", state->m_banking_bios[3]);
12241380         memory_set_bankptr(machine, "bank4", state->m_banking_bios[3] + 0x2000);
12251381         memory_set_bankptr(machine, "bank5", state->m_banking_bios[5]);
r13616r13617
12471403   {
12481404      if (!strncmp((char*)&magic[0x7ff0], "TMR SEGA", 8))
12491405      {
1250#if 0
1251         /* Technically, it should be this, but remove for now until verified: */
1252         if (!strcmp(sysname, "gamegear"))
1253         {
1254            if ((unsigned char)magic[0x7ffd] < 0x50)
1255               retval = IMAGE_VERIFY_PASS;
1256         }
1257         if (!strcmp(sysname, "sms"))
1258         {
1259            if ((unsigned char)magic[0x7ffd] >= 0x50)
1260               retval = IMAGE_VERIFY_PASS;
1261         }
1262#endif
12631406         retval = IMAGE_VERIFY_PASS;
12641407      }
12651408
1266#if 0
1267      /* Check at $81f0 also */
1268      if (!retval)
1269      {
1270         if (!strncmp(&magic[0x81f0], "TMR SEGA", 8))
1271         {
1272#if 0
1273            /* Technically, it should be this, but remove for now until verified: */
1274            if (!strcmp(sysname, "gamegear"))
1275            {
1276               if ((unsigned char)magic[0x81fd] < 0x50)
1277                  retval = IMAGE_VERIFY_PASS;
1278            }
1279            if (!strcmp(sysname, "sms"))
1280            {
1281               if ((unsigned char)magic[0x81fd] >= 0x50)
1282                  retval = IMAGE_VERIFY_PASS;
1283            }
1284#endif
1285            retval = IMAGE_VERIFY_PASS;
1286         }
1287      }
1288#endif
1289
12901409   }
12911410
12921411   return retval;
r13616r13617
15351654         {
15361655            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
15371656         }
1657         else if ( ! strcmp( mapper, "nemesis" ) )
1658         {
1659            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
1660            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_NEMESIS;
1661         }
1662         else if ( ! strcmp( mapper, "4pak" ) )
1663         {
1664            state->m_cartridge[index].features |= CF_4PAK_MAPPER;
1665         }
1666         else if ( ! strcmp( mapper, "janggun" ) )
1667         {
1668            state->m_cartridge[index].features |= CF_JANGGUN_MAPPER;
1669         }
15381670      }
15391671
15401672      // Check for gamegear cartridges with PIN 42 set to SMS mode
r13616r13617
15921724      /* Check for special cartridge features (new routine, courtesy of Omar Cornut, from MEKA)  */
15931725      if (size >= 0x8000)
15941726      {
1595         int c0002 = 0, c8000 = 0, cA000 = 0, cFFFF = 0, i;
1727         int c0002 = 0, c8000 = 0, cA000 = 0, cFFFF = 0, c3FFE = 0, c4000 = 0, c6000 = 0, i;
15961728         for (i = 0; i < 0x8000; i++)
15971729         {
15981730            if (state->m_cartridge[index].ROM[i] == 0x32) // Z80 opcode for: LD (xxxx), A
r13616r13617
16061738               { i += 2; c8000++; continue; }
16071739               if (addr == 0xA000)
16081740               { i += 2; cA000++; continue; }
1741               if ( addr == 0x3FFE)
1742               { i += 2; c3FFE++; continue; }
1743               if ( addr == 0x4000 )
1744               { i += 2; c4000++; continue; }
1745               if ( addr == 0x6000 )
1746               { i += 2; c6000++; continue; }
16091747            }
16101748         }
16111749
r13616r13617
16131751
16141752         // 2 is a security measure, although tests on existing ROM showed it was not needed
16151753         if (c0002 > cFFFF + 2 || (c0002 > 0 && cFFFF == 0))
1754         {
1755            UINT8 *rom = state->m_cartridge[index].ROM;
1756
16161757            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
1758            // Check for special bank 0 signature
1759            if ( size == 0x20000 && rom[0] == 0x00 && rom[1] == 0x00 && rom[2] == 0x00 &&
1760                 rom[0x1e000] == 0xF3 && rom[0x1e001] == 0xED && rom[0x1e002] == 0x56 )
1761            {
1762               state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_NEMESIS;
1763            }
1764         }
16171765         else if (c8000 > cFFFF + 2 || (c8000 > 0 && cFFFF == 0))
1766         {
16181767            state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1768         }
16191769         else if (cA000 > cFFFF + 2 || (cA000 > 0 && cFFFF == 0))
1770         {
16201771            state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1621
1772         }
1773         else if ( c3FFE > cFFFF + 2 || (c3FFE > 0) )
1774         {
1775            state->m_cartridge[index].features |= CF_4PAK_MAPPER;
1776         }
1777         else if ( c4000 > 0 && c6000 > 0 && c8000 > 0 && cA000 > 0 )
1778         {
1779            state->m_cartridge[index].features |= CF_JANGGUN_MAPPER;
1780         }
16221781      }
16231782   }
16241783
r13616r13617
16351794   /* Terebi Oekaki (TV Draw) is a SG1000 game with special input device which is compatible with SG1000 Mark III */
16361795   if ((detect_tvdraw(state->m_cartridge[index].ROM)) && state->m_is_region_japan)
16371796   {
1638      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1639      program->install_legacy_write_handler(0x6000, 0x6000, FUNC(sms_tvdraw_axis_w));
1640      program->install_legacy_read_handler(0x8000, 0x8000, FUNC(sms_tvdraw_status_r));
1641      program->install_legacy_read_handler(0xa000, 0xa000, FUNC(sms_tvdraw_data_r));
1642      program->nop_write(0xa000, 0xa000);
1797      state->m_cartridge[index].features |= CF_TVDRAW;
16431798   }
16441799
1645   if (state->m_cartridge[index].features & CF_KOREAN_NOBANK_MAPPER)
1646   {
1647      // FIXME: we should have by default FFFD-FFFF to be only a mirror for DFFD-DFFF (with no bankswitch logic)
1648      // and then the handlers should be installed here for all but the KOREAN_NOBANK carts
1649      // However, to avoid memory map breakage, we currently go the other way around
1650      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1651      program->install_legacy_readwrite_handler(0xfffc, 0xffff, FUNC(sms_kor_nobank_r), FUNC(sms_kor_nobank_w));
1652   }
1653
16541800   LOG(("Cart Features: %x\n", state->m_cartridge[index].features));
16551801
16561802   /* Load battery backed RAM, if available */
r13616r13617
16701816      state->m_banking_cart[2] = state->m_cartridge[state->m_current_cartridge].ROM + 0x0400;
16711817      state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
16721818      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
1819      state->m_banking_cart[7] = state->m_cartridge[state->m_current_cartridge].ROM + 0x2000;
16731820      /* Codemasters mapper points to bank 0 for page 2 */
1674      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1821      if ( state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER )
16751822      {
16761823         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM;
16771824      }
1825      /* Nemesis starts with last 8kb bank in page 0 */
1826      if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_NEMESIS )
1827      {
1828         state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM + ( rom_page_count - 1 ) * 0x4000 + 0x2000;
1829         state->m_banking_cart[2] = state->m_cartridge[state->m_current_cartridge].ROM + ( rom_page_count - 1 ) * 0x4000 + 0x2000 + 0x400;
1830      }
16781831   }
16791832   else
16801833   {
1681      state->m_banking_cart[1] = state->m_banking_none[1];
1682      state->m_banking_cart[2] = state->m_banking_none[2];
1683      state->m_banking_cart[3] = state->m_banking_none[3];
1684      state->m_banking_cart[5] = state->m_banking_none[5];
1834      state->m_banking_cart[1] = state->m_banking_none;
1835      state->m_banking_cart[2] = state->m_banking_none;
1836      state->m_banking_cart[3] = state->m_banking_none;
1837      state->m_banking_cart[5] = state->m_banking_none;
16851838   }
16861839}
16871840
r13616r13617
16891842{
16901843   sms_state *state = machine.driver_data<sms_state>();
16911844   UINT8 *mem = machine.region("maincpu")->base();
1692   state->m_banking_bios[1] = state->m_banking_cart[1] = state->m_banking_none[1] = mem;
1693   state->m_banking_bios[2] = state->m_banking_cart[2] = state->m_banking_none[2] = mem;
1694   state->m_banking_bios[3] = state->m_banking_cart[3] = state->m_banking_none[3] = mem;
1695   state->m_banking_bios[4] = state->m_banking_cart[4] = state->m_banking_none[4] = mem;
1696   state->m_banking_bios[5] = state->m_banking_cart[5] = state->m_banking_none[5] = mem;
1697   state->m_banking_bios[6] = state->m_banking_cart[6] = state->m_banking_none[6] = mem;
1845   state->m_banking_none = mem;
1846   state->m_banking_bios[1] = state->m_banking_cart[1] = mem;
1847   state->m_banking_bios[2] = state->m_banking_cart[2] = mem;
1848   state->m_banking_bios[3] = state->m_banking_cart[3] = mem;
1849   state->m_banking_bios[4] = state->m_banking_cart[4] = mem;
1850   state->m_banking_bios[5] = state->m_banking_cart[5] = mem;
1851   state->m_banking_bios[6] = state->m_banking_cart[6] = mem;
1852   state->m_banking_bios[7] = state->m_banking_cart[7] = mem;
16981853
16991854   state->m_BIOS = machine.region("user1")->base();
17001855
r13616r13617
17621917   }
17631918
17641919   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
1920   {
17651921      space->install_legacy_write_handler(0x0000, 0x0003, FUNC(sms_korean_zemina_banksw_w));
1922   }
17661923
1924   if (state->m_cartridge[state->m_current_cartridge].features & CF_JANGGUN_MAPPER)
1925   {
1926      space->install_legacy_write_handler(0x4000, 0x4000, FUNC(sms_janggun_bank0_w));
1927      space->install_legacy_write_handler(0x6000, 0x6000, FUNC(sms_janggun_bank1_w));
1928      space->install_legacy_write_handler(0x8000, 0x8000, FUNC(sms_janggun_bank2_w));
1929      space->install_legacy_write_handler(0xA000, 0xA000, FUNC(sms_janggun_bank3_w));
1930   }
1931
1932   if (state->m_cartridge[state->m_current_cartridge].features & CF_4PAK_MAPPER)
1933   {
1934      space->install_legacy_write_handler(0x3ffe, 0x3ffe, FUNC(sms_4pak_page0_w));
1935      space->install_legacy_write_handler(0x7fff, 0x7fff, FUNC(sms_4pak_page1_w));
1936      space->install_legacy_write_handler(0xbfff, 0xbfff, FUNC(sms_4pak_page2_w));
1937   }
1938
1939   if (state->m_cartridge[state->m_current_cartridge].features & CF_TVDRAW )
1940   {
1941      space->install_legacy_write_handler(0x6000, 0x6000, FUNC(sms_tvdraw_axis_w));
1942      space->install_legacy_read_handler(0x8000, 0x8000, FUNC(sms_tvdraw_status_r));
1943      space->install_legacy_read_handler(0xa000, 0xa000, FUNC(sms_tvdraw_data_r));
1944      space->nop_write(0xa000, 0xa000);
1945      state->m_cartridge[state->m_current_cartridge].m_tvdraw_data = 0;
1946   }
1947
17671948   if (state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE)
17681949   {
17691950      state->m_vdp->set_gg_sms_mode(1);
r13616r13617
18031984   state->m_lphaser_2_latch = 0;
18041985
18051986   state->m_sscope_state = 0;
1806
1807   state->m_tvdraw_data = 0;
18081987}
18091988
18101989READ8_HANDLER( sms_store_cart_select_r )
src/mess/includes/sms.h
r13616r13617
1919class sms_state : public driver_device
2020{
2121public:
22   void map_cart_16k( UINT16 address, UINT16 bank );
23   void map_cart_8k( UINT16 address, UINT16 bank );
24   void map_bios_16k( UINT16 address, UINT16 bank );
25   void map_bios_8k( UINT16 address, UINT16 bank );
26
27public:
2228   sms_state(const machine_config &mconfig, device_type type, const char *tag)
2329   : driver_device(mconfig, type, tag) { }
2430
r13616r13617
4349   // we are going to use 1-6, same as bank numbers. Notice, though, that most mappers
4450   // only work on 16K banks and, hence, banks 4-6 are not always directly set
4551   // (they often use bank3 + 0x2000 and bank5 + 0x2000)
46   UINT8 *m_banking_bios[7];
47   UINT8 *m_banking_cart[7];
48   UINT8 *m_banking_none[7];
52   UINT8 *m_banking_bios[8];
53   UINT8 *m_banking_cart[8];
54   UINT8 *m_banking_none;
4955   UINT8 m_gg_sio[5];
5056   UINT8 m_store_control;
5157   UINT8 m_input_port0;
r13616r13617
99105   /* Data needed for SegaScope (3D glasses) */
100106   UINT8 m_sscope_state;
101107
102   /* Data needed for Terebi Oekaki (TV Draw) */
103   UINT8 m_tvdraw_data;
104
105108   /* Cartridge slot info */
106109   UINT8 m_current_cartridge;
107110   struct
108111   {
109112      UINT8 *ROM;        /* Pointer to ROM image data */
110113      UINT32 size;       /* Size of the ROM image */
111      UINT8 features;    /* on-cartridge special hardware */
114      UINT32 features;   /* on-cartridge special hardware */
112115      UINT8 *cartSRAM;   /* on-cartridge SRAM */
113116      UINT8 sram_save;   /* should be the contents of the on-cartridge SRAM be saved */
114117      UINT8 *cartRAM;    /* additional on-cartridge RAM (64KB for Ernie Els Golf) */
115118      UINT32 ram_size;   /* size of the on-cartridge RAM */
116119      UINT8 ram_page;    /* currently swapped in cartridge RAM */
120
121      /* Data needed for Terebi Oekaki (TV Draw) */
122      UINT8 m_tvdraw_data;
123
124      /* Data needed for 4pak mapper */
125      UINT8 m_4pak_page0;
126      UINT8 m_4pak_page1;
127      UINT8 m_4pak_page2;
117128   } m_cartridge[MAX_CARTRIDGES];
118129};
119130
src/mess/drivers/sms.c
r13616r13617
7474
7575static ADDRESS_MAP_START( sms1_mem, AS_PROGRAM, 8 )
7676   AM_RANGE(0x0000, 0x03ff) AM_ROMBANK("bank1")               /* First 0x0400 part always points to first page */
77   AM_RANGE(0x0400, 0x3fff) AM_ROMBANK("bank2")               /* switchable rom bank */
77   AM_RANGE(0x0400, 0x1fff) AM_ROMBANK("bank2")               /* switchable rom bank */
78   AM_RANGE(0x2000, 0x3fff) AM_ROMBANK("bank7")               /* switchable rom bank */
7879   AM_RANGE(0x4000, 0x5fff) AM_ROMBANK("bank3")               /* switchable rom bank */
7980   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank4")               /* switchable rom bank */
8081   AM_RANGE(0x8000, 0x9fff) AM_READ_BANK("bank5") AM_WRITE(sms_cartram_w)   /* ROM bank / on-cart RAM */
r13616r13617
8788
8889static ADDRESS_MAP_START( sms_mem, AS_PROGRAM, 8 )
8990   AM_RANGE(0x0000, 0x03ff) AM_ROMBANK("bank1")               /* First 0x0400 part always points to first page */
90   AM_RANGE(0x0400, 0x3fff) AM_ROMBANK("bank2")               /* switchable rom bank */
91   AM_RANGE(0x0400, 0x1fff) AM_ROMBANK("bank2")               /* switchable rom bank */
92   AM_RANGE(0x2000, 0x3fff) AM_ROMBANK("bank7")               /* switchable rom bank */
9193   AM_RANGE(0x4000, 0x5fff) AM_ROMBANK("bank3")               /* switchable rom bank */
9294   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank4")               /* switchable rom bank */
9395   AM_RANGE(0x8000, 0x9fff) AM_READ_BANK("bank5") AM_WRITE(sms_cartram_w)   /* ROM bank / on-cart RAM */

Previous 15994 Revisions Next


© 1998-2018 The MESS Team