OSDN Git Service

* public snapshot of sid simulator
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / memory / at29.h
1 // at29.h - A specialisation of the generic flash memory class to
2 // model the ATMEL 29xxx series of flash memory.  -*- C++ -*-
3
4 // Copyright (C) 1999, 2000 Red Hat.
5 // This file is part of SID and is licensed under the GPL.
6 // See the file COPYING.SID for conditions for redistribution.
7
8 // The ATMEL flash has a data security mechanism that prevents
9 // unexpected data modification due to transient address/data lines in
10 // the physical realm.  To write to a given sector, a special sequence
11 // of address writes must first take place.
12
13 // More details on this component can be found on ATMEL's web site
14 // (http://www.atmel.com/) in various data sheets.
15
16 #ifndef AT29_H
17 #define AT29_H
18
19 #include "flash.h"
20
21 #include <sidcomp.h>
22 #include <sidtypes.h>
23 #include <sidattrutil.h>
24
25 #include <stdexcept>
26 #include <cassert>
27 #include <iostream>
28
29 using std::ostream;
30 using std::istream;
31 using std::string;
32
33 using sid::bus;
34 using sid::host_int_1;
35 using sid::host_int_4;
36 using sid::little_int_1;
37 using sid::little_int_2;
38 using sid::little_int_4;
39 using sid::little_int_8;
40 using sid::big_int_1;
41 using sid::big_int_2;
42 using sid::big_int_4;
43 using sid::big_int_8;
44
45 using sidutil::make_attribute;
46 using sidutil::parse_attribute;
47
48
49
50 struct at29_flash_memory_type
51 {
52   const char* name;
53   host_int_4 memory_size;
54   host_int_4 sector_size;
55   host_int_1 device_id;
56 };
57
58
59
60 class at29_flash_memory;
61
62 class at29_bus: public bus
63 {
64 public:
65   at29_bus(at29_flash_memory* target): target(target) {}
66   
67   bus::status write(host_int_4 address, big_int_1 data) throw ();
68   bus::status write(host_int_4 address, little_int_1 data) throw ();
69   bus::status read(host_int_4 address, big_int_1& data) throw ();
70   bus::status read(host_int_4 address, little_int_1& data) throw ();
71   
72 #define NOPERM_WRITE(type) \
73     bus::status write(host_int_4 address, type data) throw () \
74               { return bus::unpermitted; }
75   
76 #define NOPERM_READ(type) \
77     bus::status read(host_int_4 address, type& data) throw () \
78               { return bus::unpermitted; }
79   
80   NOPERM_WRITE(big_int_2);
81   NOPERM_WRITE(little_int_2);
82   NOPERM_WRITE(big_int_4);
83   NOPERM_WRITE(little_int_4);
84   NOPERM_WRITE(big_int_8);
85   NOPERM_WRITE(little_int_8);
86   
87   NOPERM_READ(big_int_2);
88   NOPERM_READ(little_int_2);
89   NOPERM_READ(big_int_4);
90   NOPERM_READ(little_int_4);
91   NOPERM_READ(big_int_8);
92   NOPERM_READ(little_int_8);
93   
94 #undef NOPERM_READ
95 #undef NOPERM_WRITE
96
97   private:
98     at29_flash_memory* target;
99 };
100
101
102
103 class at29_flash_memory: public flash_uniform_sector_memory
104 {
105 public:
106
107   at29_flash_memory(size_t memory_size, size_t sector_size, 
108                     host_int_1 device_id) throw (bad_alloc);
109   static const at29_flash_memory_type types[];
110
111 protected:
112   void stream_state (ostream&) const;
113   void destream_state (istream&);  
114   
115 private:
116   friend class at29_bus;
117   at29_bus my_bus;
118
119   bool write_ok(host_int_4 address);
120
121   enum states {
122     LOCKED,
123     START1,
124     START2,
125     PROG,
126     IDENT
127   } state;
128
129   const host_int_4 noSectorNum;
130   host_int_4 sectorNum;
131   host_int_1 manufacturerCode;
132   host_int_1 deviceIdCode;
133 };
134
135
136 #endif // AT29_H