1//============================================================================
2//
3// SSSS tt lll lll
4// SS SS tt ll ll
5// SS tttttt eeee ll ll aaaa
6// SSSS tt ee ee ll ll aa
7// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8// SS SS tt ee ll ll aa aa
9// SSSS ttt eeeee llll llll aaaaa
10//
11// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
12// and the Stella Team
13//
14// See the file "License.txt" for information on usage and redistribution of
15// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16//============================================================================
17
18#include "Console.hxx"
19#include "Cart.hxx"
20#include "OSystem.hxx"
21#include "BankRomCheat.hxx"
22
23// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
24BankRomCheat::BankRomCheat(OSystem& os, const string& name, const string& code)
25 : Cheat(os, name, code)
26{
27 if(myCode.length() == 7)
28 myCode = "0" + code;
29
30 bank = unhex(myCode.substr(0, 2));
31 address = 0xf000 + unhex(myCode.substr(2, 3));
32 value = uInt8(unhex(myCode.substr(5, 2)));
33 count = uInt8(unhex(myCode.substr(7, 1)) + 1);
34
35 // Back up original data; we need this if the cheat is ever disabled
36 for(int i = 0; i < count; ++i)
37 savedRom[i] = myOSystem.console().cartridge().peek(address + i);
38}
39
40// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41bool BankRomCheat::enable()
42{
43 evaluate();
44 return myEnabled;
45}
46
47// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48bool BankRomCheat::disable()
49{
50 int oldBank = myOSystem.console().cartridge().getBank(address);
51 myOSystem.console().cartridge().bank(bank);
52
53 for(int i = 0; i < count; ++i)
54 myOSystem.console().cartridge().patch(address + i, savedRom[i]);
55
56 myOSystem.console().cartridge().bank(oldBank);
57
58 return myEnabled = false;
59}
60
61// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62void BankRomCheat::evaluate()
63{
64 if(!myEnabled)
65 {
66 int oldBank = myOSystem.console().cartridge().getBank(address);
67 myOSystem.console().cartridge().bank(bank);
68
69 for(int i = 0; i < count; ++i)
70 myOSystem.console().cartridge().patch(address + i, value);
71
72 myOSystem.console().cartridge().bank(oldBank);
73
74 myEnabled = true;
75 }
76}
77