a2-memory-map 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Cargo.lock ADDED
@@ -0,0 +1,88 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 3
4
+
5
+ [[package]]
6
+ name = "a2-memory-map"
7
+ version = "1.0.4"
8
+ dependencies = [
9
+ "serde_json",
10
+ ]
11
+
12
+ [[package]]
13
+ name = "itoa"
14
+ version = "1.0.11"
15
+ source = "registry+https://github.com/rust-lang/crates.io-index"
16
+ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
17
+
18
+ [[package]]
19
+ name = "proc-macro2"
20
+ version = "1.0.82"
21
+ source = "registry+https://github.com/rust-lang/crates.io-index"
22
+ checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b"
23
+ dependencies = [
24
+ "unicode-ident",
25
+ ]
26
+
27
+ [[package]]
28
+ name = "quote"
29
+ version = "1.0.36"
30
+ source = "registry+https://github.com/rust-lang/crates.io-index"
31
+ checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
32
+ dependencies = [
33
+ "proc-macro2",
34
+ ]
35
+
36
+ [[package]]
37
+ name = "ryu"
38
+ version = "1.0.18"
39
+ source = "registry+https://github.com/rust-lang/crates.io-index"
40
+ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
41
+
42
+ [[package]]
43
+ name = "serde"
44
+ version = "1.0.202"
45
+ source = "registry+https://github.com/rust-lang/crates.io-index"
46
+ checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
47
+ dependencies = [
48
+ "serde_derive",
49
+ ]
50
+
51
+ [[package]]
52
+ name = "serde_derive"
53
+ version = "1.0.202"
54
+ source = "registry+https://github.com/rust-lang/crates.io-index"
55
+ checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
56
+ dependencies = [
57
+ "proc-macro2",
58
+ "quote",
59
+ "syn",
60
+ ]
61
+
62
+ [[package]]
63
+ name = "serde_json"
64
+ version = "1.0.117"
65
+ source = "registry+https://github.com/rust-lang/crates.io-index"
66
+ checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3"
67
+ dependencies = [
68
+ "itoa",
69
+ "ryu",
70
+ "serde",
71
+ ]
72
+
73
+ [[package]]
74
+ name = "syn"
75
+ version = "2.0.64"
76
+ source = "registry+https://github.com/rust-lang/crates.io-index"
77
+ checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f"
78
+ dependencies = [
79
+ "proc-macro2",
80
+ "quote",
81
+ "unicode-ident",
82
+ ]
83
+
84
+ [[package]]
85
+ name = "unicode-ident"
86
+ version = "1.0.12"
87
+ source = "registry+https://github.com/rust-lang/crates.io-index"
88
+ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
package/Cargo.toml ADDED
@@ -0,0 +1,17 @@
1
+ [package]
2
+ name = "a2-memory-map"
3
+ description = "Database of Apple II memory addresses"
4
+ version = "1.0.4"
5
+ keywords = ["retro", "apple"]
6
+ categories = []
7
+ repository = "https://github.com/dfgordon/a2-memory-map"
8
+ homepage = "https://github.com/dfgordon/a2-memory-map"
9
+ readme = "README-crate.md"
10
+ edition = "2021"
11
+ license = "MIT"
12
+
13
+ [lib]
14
+ path = "bindings/rust/lib.rs"
15
+
16
+ [dependencies]
17
+ serde_json = "1.0.116"
@@ -0,0 +1,65 @@
1
+ # Apple II Memory Map
2
+
3
+ ![unit tests](https://github.com/dfgordon/a2-memory-map/actions/workflows/rust.yml/badge.svg)
4
+
5
+ This is the rust binding to a database of Apple II special addresses intended to be used with language servers. The central element is the file `map.json` which maps addresses to a set of descriptive strings.
6
+
7
+ ## Map Records
8
+
9
+ The map records correspond to the structure
10
+
11
+ ```rs
12
+ pub struct AddressInfo {
13
+ pub brief: Option<String>,
14
+ pub ctx: Option<String>,
15
+ pub desc: String,
16
+ pub label: Option<String>,
17
+ pub note: Option<String>,
18
+ pub subctx: Option<String>,
19
+ pub typ: String
20
+ }
21
+ ```
22
+
23
+ Information is accessed through the opaque `struct MemoryMap`. The whole database can be borrowed as a `HashMap` as follows:
24
+
25
+ ```rs
26
+ let a2map = MemoryMap::new();
27
+ let full_map: &HashMap<u16,AddressInfo> = a2map.get_all();
28
+ ```
29
+
30
+ To get a single record use
31
+
32
+ ```rs
33
+ let maybe_info: Option<AddressInfo> = a2map.get_one(addr);
34
+ ```
35
+
36
+ The argument `addr` can range from -32767 to 65535. As an example `get_one(0xfded)` will give the rust equivalent of
37
+
38
+ ```js
39
+ {
40
+ brief: 'Print character in A',
41
+ desc: 'Invoke output routine whose address is in (56). Usually prints character in A.',
42
+ label: 'COUT',
43
+ type: 'ROM routine'
44
+ }
45
+ ```
46
+
47
+ ## AddressInfo Fields
48
+
49
+ * `brief`: short description suitable for display in a selection box
50
+ * `ctx`: notes on limiting context, such as hardware requirements, applicability to given language, etc.
51
+ * `desc`: long description suitable for hovers
52
+ * `label`: suggested assembler label
53
+ * `note`: any other notes
54
+ * `subctx`: notes on sub context, e.g., specific aspect of a language
55
+ * `type`: data type (e.g. `word`) or operation type (e.g. `ROM routine`, `soft switch`)
56
+
57
+ ## Multi-context Records
58
+
59
+ The `map.json` file uses logic expressions to delineate information by context, e.g., `ctx` might have the value `"Applesoft | Integer BASIC"`. The rust binding provides a function to parse the logic and produce an array of records, with each element corresponding to a given context:
60
+
61
+ ```rs
62
+ let maybe_records: Option<Vec<AddressInfo>> = a2map.get_one_and_split(addr);
63
+ ```
64
+
65
+ If there is no splitting of the `ctx` field, no other fields will be split. It is not required to use this function. If `get_one` is used instead, downstream will receive the logic expressions, which can be parsed in any manner desired.
package/README.md CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  ![unit tests](https://github.com/dfgordon/a2-memory-map/actions/workflows/node.js.yml/badge.svg)
4
4
 
5
- This is a database of Apple II special addresses intended to be used with language servers. The central element is the file `map.json` which maps addresses to a set of descriptive strings.
5
+ This is the TypeScript binding to a database of Apple II special addresses intended to be used with language servers. The central element is the file `map.json` which maps addresses to a set of descriptive strings.
6
6
 
7
- ## TypeScript Interface
7
+ ## Map Records
8
8
 
9
- There is a [node package](https://www.npmjs.com/package/a2-memory-map) that can be used to access the data. The map records correspond to the interface
9
+ The map records correspond to the interface
10
10
 
11
11
  ```ts
12
12
  interface AddressInfo {
@@ -32,7 +32,7 @@ To get a single record use
32
32
  get_one(addr: number): AddressInfo | undefined
33
33
  ```
34
34
 
35
- The argument `addr` can range from -32767 to 65535. As an example `get_info(0xfded)` will give
35
+ The argument `addr` can range from -32767 to 65535. As an example `get_one(0xfded)` will give
36
36
 
37
37
  ```js
38
38
  {
@@ -61,12 +61,4 @@ The `map.json` file uses logic expressions to delineate information by context,
61
61
  get_one_and_split(addr: number): Array<AddressInfo> | undefined
62
62
  ```
63
63
 
64
- If there is no splitting of the `ctx` field, no other fields will be split. It is not required to use this function. If `get_one` is used instead, downstream will receive the logic expressions, which can be parsed in any manner desired.
65
-
66
- ## References
67
-
68
- Apple's own documentation is a good starting point. The "Apple IIe Reference Manual" has low level information. The "Applesoft BASIC Programmer's Reference Manual" provides a chapter "Pokes, Peeks, and Calls" with information especially useful to BASIC programmers.
69
-
70
- "All About Applesoft" from [Call-A.P.P.L.E.](https://www.callapple.org/) has a more in depth treatment.
71
-
72
- Sometimes an ambiguity is best resolved by studying the relevant disassembly.
64
+ If there is no splitting of the `ctx` field, no other fields will be split. It is not required to use this function. If `get_one` is used instead, downstream will receive the logic expressions, which can be parsed in any manner desired.
@@ -0,0 +1,192 @@
1
+ use serde_json;
2
+ use std::collections::HashMap;
3
+
4
+ #[cfg(windows)]
5
+ const JSON_MAP: &str = include_str!("..\\..\\src\\map.json");
6
+ #[cfg(not(windows))]
7
+ const JSON_MAP: &str = include_str!("../../src/map.json");
8
+
9
+ #[derive(Clone)]
10
+ pub struct AddressInfo {
11
+ pub brief: Option<String>,
12
+ pub ctx: Option<String>,
13
+ pub desc: String,
14
+ pub label: Option<String>,
15
+ pub note: Option<String>,
16
+ pub subctx: Option<String>,
17
+ pub typ: String
18
+ }
19
+
20
+ /// All interactions are through this object
21
+ pub struct MemoryMap {
22
+ memmap: HashMap<u16,AddressInfo>
23
+ }
24
+
25
+ fn get_val(key: &str,val: &serde_json::Value) -> Option<String> {
26
+ match &val[key] {
27
+ serde_json::Value::String(s) => Some(s.to_string()),
28
+ _ => None
29
+ }
30
+ }
31
+ fn get_contextual_str(str: &String, count: usize) -> String {
32
+ let contexts: Vec<_> = str.split('|').collect();
33
+ if count < contexts.len() {
34
+ return contexts[count].trim().to_string();
35
+ }
36
+ return str.to_string();
37
+ }
38
+ fn get_contextual_str_maybe(maybe_str: &Option<String>, count: usize) -> Option<String> {
39
+ if let Some(str) = maybe_str {
40
+ let contexts: Vec<_> = str.split('|').collect();
41
+ if count < contexts.len() {
42
+ return Some(contexts[count].trim().to_string());
43
+ }
44
+ }
45
+ return None;
46
+ }
47
+ fn get_contextual_info(info: &AddressInfo, count: usize) -> AddressInfo {
48
+ AddressInfo {
49
+ brief: get_contextual_str_maybe(&info.brief,count),
50
+ ctx: get_contextual_str_maybe(&info.ctx,count),
51
+ desc: get_contextual_str(&info.desc,count),
52
+ subctx: get_contextual_str_maybe(&info.subctx,count),
53
+ label: get_contextual_str_maybe(&info.label,count),
54
+ note: get_contextual_str_maybe(&info.note,count),
55
+ typ: get_contextual_str(&info.typ,count)
56
+ }
57
+ }
58
+
59
+ impl MemoryMap {
60
+ pub fn new() -> Self {
61
+ let mut m = Self {
62
+ memmap: HashMap::new()
63
+ };
64
+ let temp = serde_json::from_str::<serde_json::Value>(JSON_MAP).unwrap();
65
+ if let Some(obj) = temp.as_object() {
66
+ for (key,val) in obj {
67
+ let addr = u16::from_str_radix(&key[2..],16).expect("bad address in memory map");
68
+ let addr_info = AddressInfo {
69
+ brief: get_val("brief",val),
70
+ ctx: get_val("ctx",val),
71
+ desc: get_val("desc",val).unwrap(),
72
+ subctx: get_val("subctx",val),
73
+ label: get_val("label",val),
74
+ note: get_val("note",val),
75
+ typ: get_val("type",val).unwrap(),
76
+ };
77
+ m.memmap.insert(addr,addr_info);
78
+ }
79
+ }
80
+ assert!(m.memmap.len()>0);
81
+ m
82
+ }
83
+ /// Borrow the underlying HashMap
84
+ pub fn get_all(&self) -> &HashMap<u16,AddressInfo> {
85
+ return &self.memmap;
86
+ }
87
+ /// Get data for one address.
88
+ /// @param addr address in the range -32767 to 65535
89
+ pub fn get_one(&self,addr: i64) -> Option<AddressInfo> {
90
+ let pos_addr = match addr < 0 { true => addr + 1 + u16::MAX as i64, false => addr };
91
+ if pos_addr >=0 && pos_addr < u16::MAX as i64 {
92
+ self.memmap.get(&(pos_addr as u16)).cloned()
93
+ } else {
94
+ None
95
+ }
96
+ }
97
+ /// Get data for one address and split by context
98
+ /// @param addr Array of AddressInfo objects, one for each context, or undefined
99
+ pub fn get_one_and_split(&self,addr: i64) -> Option<Vec<AddressInfo>> {
100
+ if let Some(obj) = self.get_one(addr) {
101
+ let mut ans = Vec::new();
102
+ let num = match &obj.ctx {
103
+ Some(ctx) => ctx.split('|').collect::<Vec<_>>().len(),
104
+ None => 1
105
+ };
106
+ // if one context do not split anything
107
+ if num == 1 {
108
+ ans.push(obj);
109
+ return Some(ans);
110
+ }
111
+ // if multi-context, split all fields on `|`
112
+ for count in 0..num {
113
+ ans.push(get_contextual_info(&obj, count));
114
+ }
115
+ return Some(ans);
116
+ }
117
+ None
118
+ }
119
+ }
120
+
121
+ #[test]
122
+ fn get_cout() {
123
+ let mem_map = MemoryMap::new();
124
+ let cout = mem_map.get_one(0xfded);
125
+ assert!(cout.unwrap().label==Some("COUT".to_string()));
126
+ }
127
+
128
+ #[test]
129
+ fn applesoft_integer() {
130
+ let mem_map = MemoryMap::new();
131
+ let addr = 0xf8;
132
+ match mem_map.get_one_and_split(addr) {
133
+ Some(info_list) => {
134
+ assert_eq!(info_list.len(),2);
135
+ assert_eq!(info_list[0].ctx,Some("Applesoft".to_string()));
136
+ assert_eq!(info_list[1].ctx,Some("Integer BASIC".to_string()));
137
+ assert_eq!(info_list[0].typ,"byte value".to_string());
138
+ assert_eq!(info_list[1].typ,"float32".to_string());
139
+ assert_eq!(info_list[0].label,Some("REMSTK".to_string()));
140
+ assert_eq!(info_list[1].label,Some("FP1".to_string()));
141
+ },
142
+ None => panic!("address not found")
143
+ }
144
+ let addr = 0xdc;
145
+ match mem_map.get_one_and_split(addr) {
146
+ Some(info_list) => {
147
+ assert_eq!(info_list.len(),2);
148
+ assert_eq!(info_list[0].ctx,Some("Applesoft".to_string()));
149
+ assert_eq!(info_list[1].ctx,Some("Integer BASIC".to_string()));
150
+ assert_eq!(info_list[0].typ,"word".to_string());
151
+ assert_eq!(info_list[1].typ,"word".to_string());
152
+ assert_eq!(info_list[0].label,Some("ERRPOS".to_string()));
153
+ assert_eq!(info_list[1].label,Some("CURLIN".to_string()));
154
+ },
155
+ None => panic!("address not found")
156
+ }
157
+ }
158
+
159
+ #[test]
160
+ fn no_split() {
161
+ let mem_map = MemoryMap::new();
162
+ let addr = 0xff70;
163
+ match mem_map.get_one_and_split(addr) {
164
+ Some(info_list) => {
165
+ assert_eq!(info_list.len(),1);
166
+ assert_eq!(info_list[0].typ,"ROM routine".to_string());
167
+ },
168
+ None => panic!("address not found")
169
+ }
170
+ let addr = 0xe597;
171
+ match mem_map.get_one_and_split(addr) {
172
+ Some(info_list) => {
173
+ assert_eq!(info_list.len(),1);
174
+ assert_eq!(info_list[0].typ,"ROM routine".to_string());
175
+ assert_eq!(info_list[0].subctx,Some("parsing & string".to_string()));
176
+ },
177
+ None => panic!("address not found")
178
+ }
179
+ }
180
+
181
+ #[test]
182
+ fn splits_but_one_ctx() {
183
+ let mem_map = MemoryMap::new();
184
+ let addr = 0xc080;
185
+ match mem_map.get_one_and_split(addr) {
186
+ Some(info_list) => {
187
+ assert_eq!(info_list.len(),1);
188
+ assert_eq!(info_list[0].brief,Some("RDRAM | stepper 0".to_string()));
189
+ },
190
+ None => panic!("address not found")
191
+ }
192
+ }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/dfgordon/a2-memory-map.git"
6
6
  },
7
- "version": "1.0.3",
7
+ "version": "1.0.4",
8
8
  "description": "Database of Apple II memory addresses",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",