fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * pce *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/lib/iniram.c *
7 * Created: 2005-07-24 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2005-2010 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#include <stdlib.h>
24
25#include <lib/iniram.h>
26#include <lib/load.h>
27#include <lib/log.h>
28#include <lib/path.h>
29#include <libini/libini.h>
30
31
32int ini_get_ram (memory_t *mem, ini_sct_t *ini, mem_blk_t **addr0)
33{
34 ini_sct_t *sct;
35 mem_blk_t *ram;
36 const char *fname;
37 char *path;
38 unsigned val;
39 unsigned long base, size;
40
41 if (addr0 != NULL) {
42 *addr0 = NULL;
43 }
44
45 sct = NULL;
46 while ((sct = ini_next_sct (ini, sct, "ram")) != NULL) {
47 ini_get_string (sct, "file", &fname, NULL);
48 if (ini_get_uint32 (sct, "address", &base, 0)) {
49 ini_get_uint32 (sct, "base", &base, 0);
50 }
51
52 if (ini_get_uint32 (sct, "sizem", &size, 0) == 0) {
53 size *= 1024UL * 1024UL;
54 }
55 else if (ini_get_uint32 (sct, "sizek", &size, 0) == 0) {
56 size *= 1024UL;
57 }
58 else {
59 ini_get_uint32 (sct, "size", &size, 65536);
60 }
61
62 ini_get_uint16 (sct, "default", &val, 0);
63
64 path = pce_path_get (fname);
65
66 pce_log_tag (MSG_INF, "RAM:", "addr=0x%08lx size=%lu file=%s\n",
67 base, size, (path == NULL) ? "<none>" : path
68 );
69
70 ram = mem_blk_new (base, size, 1);
71 if (ram == NULL) {
72 pce_log (MSG_ERR, "*** memory block creation failed\n");
73 free (path);
74 return (1);
75 }
76
77 mem_blk_clear (ram, val);
78 mem_blk_set_readonly (ram, 0);
79 mem_add_blk (mem, ram, 1);
80
81 if ((addr0 != NULL) && (base == 0)) {
82 *addr0 = ram;
83 }
84
85 if (path != NULL) {
86 if (pce_load_blk_bin (ram, path)) {
87 pce_log (MSG_ERR,
88 "*** loading ram failed (%s)\n", path
89 );
90 free (path);
91 return (1);
92 }
93 }
94
95 free (path);
96 }
97
98 return (0);
99}
100
101int ini_get_rom (memory_t *mem, ini_sct_t *ini)
102{
103 ini_sct_t *sct;
104 mem_blk_t *rom;
105 const char *fname;
106 char *path;
107 unsigned val;
108 unsigned long base, size;
109
110 sct = NULL;
111 while ((sct = ini_next_sct (ini, sct, "rom")) != NULL) {
112 ini_get_string (sct, "file", &fname, NULL);
113 if (ini_get_uint32 (sct, "address", &base, 0)) {
114 ini_get_uint32 (sct, "base", &base, 0);
115 }
116
117 if (ini_get_uint32 (sct, "sizem", &size, 0) == 0) {
118 size *= 1024UL * 1024UL;
119 }
120 else if (ini_get_uint32 (sct, "sizek", &size, 0) == 0) {
121 size *= 1024UL;
122 }
123 else {
124 ini_get_uint32 (sct, "size", &size, 65536);
125 }
126
127 ini_get_uint16 (sct, "default", &val, 0);
128
129 path = pce_path_get (fname);
130
131 pce_log_tag (MSG_INF, "ROM:", "addr=0x%08lx size=%lu file=%s\n",
132 base, size, (path != NULL) ? path : "<none>"
133 );
134
135 rom = mem_blk_new (base, size, 1);
136 if (rom == NULL) {
137 pce_log (MSG_ERR, "*** memory block creation failed\n");
138 free (path);
139 return (1);
140 }
141
142 mem_blk_clear (rom, val);
143 mem_blk_set_readonly (rom, 1);
144 mem_add_blk (mem, rom, 1);
145
146 if (path != NULL) {
147 if (pce_load_blk_bin (rom, path)) {
148 pce_log (MSG_ERR,
149 "*** loading rom failed (%s)\n", path
150 );
151 free (path);
152 return (1);
153 }
154 }
155
156 free (path);
157 }
158
159 return (0);
160}