1 // license:BSD-3-Clause
2 // copyright-holders:Nicola Salmoria, Andreas Naive, Charles MacDonald
3 /***************************************************************************
4 
5     Hitachi FD1089A/FD1089B encryption emulation
6 
7 ****************************************************************************
8 
9     Copyright Nicola Salmoria, Andreas Naive, and Charles MacDonald.
10 
11     All rights reserved.
12 
13 ***************************************************************************/
14 
15 #ifndef MAME_MACHINE_FD1089_H
16 #define MAME_MACHINE_FD1089_H
17 
18 #pragma once
19 
20 #include "cpu/m68000/m68000.h"
21 
22 
23 //**************************************************************************
24 //  CONSTANTS
25 //**************************************************************************
26 
27 // device type definition
DECLARE_DEVICE_TYPE(FD1089A,fd1089a_device)28 DECLARE_DEVICE_TYPE(FD1089A, fd1089a_device)
29 DECLARE_DEVICE_TYPE(FD1089B, fd1089b_device)
30 
31 
32 
33 //**************************************************************************
34 //  TYPE DEFINITIONS
35 //**************************************************************************
36 
37 // ======================> fd1089_base_device
38 
39 // base device, shared implementation between A and B variants
40 class fd1089_base_device : public m68000_device
41 {
42 public:
43 	// explicit decryption helpers
44 	void decrypt(offs_t baseaddr, uint32_t size, offs_t regionoffs, uint16_t *opcodesptr, uint16_t *dataptr) { decrypt(baseaddr, size, &m_plaintext[regionoffs/2], opcodesptr, dataptr); }
45 
46 	void decrypted_opcodes_map(address_map &map);
47 
48 protected:
49 	// construction/destruction
50 	fd1089_base_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock);
51 
52 	// device overrides
53 	virtual void device_start() override;
54 
55 	// internal helpers
56 	uint8_t rearrange_key(uint8_t table, bool opcode);
57 	virtual uint8_t decode(uint8_t val, uint8_t key, bool opcode) = 0;
58 	uint16_t decrypt_one(offs_t addr, uint16_t val, const uint8_t *key, bool opcode);
59 	void decrypt(offs_t baseaddr, uint32_t size, const uint16_t *srcptr, uint16_t *opcodesptr, uint16_t *dataptr);
60 
61 	// internal state
62 	required_memory_region       m_region;
63 	required_region_ptr<uint8_t>   m_key;
64 	std::vector<uint16_t>          m_plaintext;
65 	required_shared_ptr<uint16_t>  m_decrypted_opcodes;
66 
67 	// internal types
68 	struct decrypt_parameters
69 	{
70 		uint8_t xorval;
71 		uint8_t s7,s6,s5,s4,s3,s2,s1,s0;
72 	};
73 
74 	// static tables
75 	static const uint8_t s_basetable_fd1089[0x100];
76 	static const decrypt_parameters s_addr_params[16];
77 	static const decrypt_parameters s_data_params_a[16];
78 };
79 
80 
81 // ======================> fd1089a_device
82 
83 // FD1089A variant
84 class fd1089a_device : public fd1089_base_device
85 {
86 public:
87 	// construction/destruction
88 	fd1089a_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
89 
90 protected:
91 	virtual uint8_t decode(uint8_t val, uint8_t key, bool opcode) override;
92 };
93 
94 
95 // ======================> fd1089b_device
96 
97 // FD1089B variant
98 class fd1089b_device : public fd1089_base_device
99 {
100 public:
101 	// construction/destruction
102 	fd1089b_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
103 
104 protected:
105 	virtual uint8_t decode(uint8_t val, uint8_t key, bool opcode) override;
106 };
107 
108 
109 #endif // MAME_MACHINE_FD1089_H
110