-
Notifications
You must be signed in to change notification settings - Fork 0
/
disc_burning_mac.odin
346 lines (311 loc) · 13.3 KB
/
disc_burning_mac.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import "core:mem"
import "core:runtime"
import "core:fmt"
import NS "vendor:darwin/Foundation"
DiscBurningThreadContext :: struct {
disc: DreamcastDisc,
track_map: map[DRTrackRef]Track,
percentage: f64,
did_print_error: bool,
ctx: runtime.Context,
arena: mem.Arena,
}
burning_thread_context: DiscBurningThreadContext
init_burner :: proc() -> DRBurnRef {
burner_devices := DRCopyDeviceArray()
defer CFRelease(burner_devices)
burner_devices_len := CFArrayGetCount(burner_devices)
if (burner_devices_len == 0) {
burn_session_error("No CD burners available!")
}
device := cast(DRDeviceRef)CFArrayGetValueAtIndex(burner_devices, 0)
burner := DRBurnCreate(device)
//lowest speed
CD_1X :: 150
burner_speed := cfnum(CD_1X)
defer CFRelease(burner_speed)
burner_properties := cfdictionary(
kDRBurnRequestedSpeedKey, burner_speed,
kDRBurnStrategyKey, kDRBurnStrategyCDSAO,
kDRBurnStrategyIsRequiredKey, kCFBooleanTrue,
)
defer CFRelease(burner_properties)
DRBurnSetProperties(burner, burner_properties)
return burner
}
burn_handle_notification :: proc "c" (center: DRNotificationCenterRef,
observer: rawptr, name: CFStringRef, object: rawptr, info: CFDictionaryRef) {
context = burning_thread_context.ctx
print_verbose("Burn notification name: %v, info %v", name, info)
status_state := cast(CFStringRef)CFDictionaryGetValue(info, kDRStatusStateKey)
progress_cfnum :=
cast(CFNumberRef)CFDictionaryGetValue(info, kDRStatusPercentCompleteKey)
if progress_cfnum != nil {
progress: f64
CFNumberGetValue(progress_cfnum, .kCFNumberFloat64Type, &progress)
if progress >= 0 {
fmt.printf("Progress: %.2f%%\r", progress * 100)
}
} else if error_status := cast(CFDictionaryRef)CFDictionaryGetValue(info, kDRErrorStatusKey);
error_status != nil && !burning_thread_context.did_print_error {
defer burning_thread_context.did_print_error = true
error_code_cfnum := cast(CFNumberRef)CFDictionaryGetValue(error_status, kDRErrorStatusErrorKey)
error_code: OSStatus
CFNumberGetValue(error_code_cfnum, .kCFNumberLongType, &error_code)
error_string := cast(CFStringRef)CFDictionaryGetValue(error_status, kDRErrorStatusErrorStringKey)
error_additional_string := cast(CFStringRef)CFDictionaryGetValue(error_status, kDRErrorStatusAdditionalSenseStringKey)
print("\nError burning disc! Code: 0x%X", error_code)
if error_additional_string != nil {
print("\t%v -- %v", error_string, error_additional_string)
} else {
print("\t%v", error_string)
}
}
if CFStringCompare(status_state, kDRStatusStateFailed, 0) == 0 {
print("\nBurn failed!")
CFRunLoopStop(CFRunLoopGetCurrent())
}
if CFStringCompare(status_state, kDRStatusStateDone, 0) == 0 {
fmt.printf("Progress: %.2f%%\r", 100.0)
print("\nBurn success!")
CFRunLoopStop(CFRunLoopGetCurrent())
}
}
//where all the magic happens
burn_callback :: proc "c" (drtrack: DRTrackRef, message: DRTrackMessage, io_param: rawptr) -> OSStatus {
DISABLE_WRITING :: false
DRTrackProductionInfo :: struct {
buffer: rawptr, //In - The buffer to produce into.
req_count: u32, //In - The number of bytes requested by the engine.
act_count: u32, //Out - The number of bytes actually produced (between 0 and reqCount)
flags: u32, //InOut - Miscellaneous flags.
block_size: u32, //In - The block size the engine is expecting.
requested_address: u64, //In - The byte address that the burn engine is requesting from the
//object (0-based). This increments when you send data, as one
//would expect.
}
write_data :: proc(prod_info: ^DRTrackProductionInfo, track_data: []byte) -> bool {
when DISABLE_WRITING {
return false
}
requested_address := cast(int)prod_info.requested_address
req_count := cast(int)prod_info.req_count
if requested_address+req_count > len(track_data) {
print("Requested address %v with count %v is out of bounds for data of len %v",
requested_address, req_count, len(track_data))
return false
}
dst := (cast([^]byte)prod_info.buffer)[:req_count]
src := track_data[requested_address:requested_address+req_count]
copy(dst, src)
prod_info.act_count = auto_cast req_count
return true
}
write_pregap :: proc(prod_info: ^DRTrackProductionInfo) -> bool {
when DISABLE_WRITING {
return false
}
requested_address := cast(int)prod_info.requested_address
req_count := cast(int)prod_info.req_count
dst := (cast([^]byte)prod_info.buffer)[:req_count]
mem.zero_slice(dst)
prod_info.act_count = auto_cast req_count
return true
}
verify_data :: proc(prod_info: ^DRTrackProductionInfo, track_data: []byte) -> bool {
when DISABLE_WRITING {
return false
}
requested_address := cast(int)prod_info.requested_address
req_count := cast(int)prod_info.req_count
if requested_address+req_count > len(track_data) {
return false
}
disc_data := (cast([^]byte)prod_info.buffer)[:req_count]
image_data := track_data[requested_address:requested_address+req_count]
return mem.compare(disc_data, image_data) == 0
}
verify_pregap :: proc(prod_info: ^DRTrackProductionInfo) -> bool {
when DISABLE_WRITING {
return false
}
requested_address := cast(int)prod_info.requested_address
req_count := cast(int)prod_info.req_count
disc_data := (cast([^]byte)prod_info.buffer)[:req_count]
for b in disc_data {
if b != 0 {
return false
}
}
return true
}
noErr :: 0
kDRFunctionNotSupportedErr :: 0x80020067
kDRVerificationFailedErr :: 0x80020063
kDRDataProductionErr :: 0x80020062
context = burning_thread_context.ctx
track := burning_thread_context.track_map[drtrack]
switch message {
case .kDRTrackMessagePreBurn:
return noErr
case .kDRTrackMessageProduceData:
prod_info := cast(^DRTrackProductionInfo)io_param
if prod_info.flags != 0 {
print("Non-zero flags detected for data production: %v", prod_info)
return kDRFunctionNotSupportedErr
}
{
i := cast(int)prod_info.requested_address
n := cast(int)prod_info.req_count
drtrack_properties := DRTrackGetProperties(drtrack)
print_verbose("Writing data bytes %v to %v for track %v", i, i + n, track.number)
print_verbose("\tTrack properties: %v", drtrack_properties)
}
return write_data(prod_info, track.sectors) ? noErr : kDRDataProductionErr
case .kDRTrackMessageVerificationStarting:
case .kDRTrackMessageVerificationDone:
case .kDRTrackMessageVerifyData:
prod_info := cast(^DRTrackProductionInfo)io_param
return verify_data(prod_info, track.sectors) ? noErr : kDRVerificationFailedErr
case .kDRTrackMessagePostBurn:
return noErr
case .kDRTrackMessageEstimateLength:
case .kDRTrackMessageProducePreGap:
if track.number_of_pregap_bytes == 0 {
return kDRFunctionNotSupportedErr
}
prod_info := cast(^DRTrackProductionInfo)io_param
if prod_info.flags != 0 {
print("Non-zero flags detected for pregap production: %v", prod_info)
return kDRFunctionNotSupportedErr
}
{
i := cast(int)prod_info.requested_address
n := cast(int)prod_info.req_count
print_verbose("Writing pregap bytes %v to %v for track %v", i, i + n, track.number)
print_verbose("\tBlock size:%v, Start LBA: %v", prod_info.block_size, track.start_lba)
}
//return write_data(prod_info, track.pregap_sectors) ? noErr : kDRDataProductionErr
return write_pregap(prod_info) ? noErr : kDRDataProductionErr
// track_data := track.pregap_sectors
// write_data(prod_info, track_data)
// NSLog(NS.AT("Produce Pregap"))
case .kDRTrackMessageVerifyPreGap:
prod_info := cast(^DRTrackProductionInfo)io_param
//return verify_data(prod_info, track.pregap_sectors) ? noErr : kDRVerificationFailedErr
return verify_pregap(prod_info) ? noErr : kDRVerificationFailedErr
// NSLog(NS.AT("Verify Pregap"))
}
print("Unsupported burn message: %v", message)
return kDRFunctionNotSupportedErr
}
burn_dreamcast_disc :: proc(disc: DreamcastDisc) {
scoped_temp_memory()
burning_thread_context = {
ctx = runtime.default_context(),
disc = disc,
}
mem.arena_init(&burning_thread_context.arena, make([]byte, 4 * mem.Megabyte))
burning_thread_context.ctx.allocator = mem.arena_allocator(&burning_thread_context.arena)
burning_thread_context.ctx.user_ptr = context.user_ptr
//package audio session into CFArray
audio_session_layout: CFArrayRef
defer CFRelease(audio_session_layout)
{
session := disc.audio_session
drtrack_slice := make([]DRTrackRef, len(session.tracks))
for track, i in session.tracks {
drtrack_slice[i] = create_drtrack(track)
burning_thread_context.track_map[drtrack_slice[i]] = track
}
audio_session_layout = cfarray(drtrack_slice)
}
//package data session into CFArray
data_session_layout: CFArrayRef
defer CFRelease(data_session_layout)
{
session := disc.data_session
drtrack_slice := make([]DRTrackRef, len(session.tracks))
for track, i in session.tracks {
drtrack_slice[i] = create_drtrack(track)
burning_thread_context.track_map[drtrack_slice[i]] = track
}
data_session_layout = cfarray(drtrack_slice)
}
//package disc layout
disc_layout: CFArrayRef
defer CFRelease(disc_layout)
{
sessions := [2]CFArrayRef{audio_session_layout, data_session_layout}
disc_layout = cfarray(sessions[:])
}
burner := init_burner()
defer CFRelease(burner)
notification_center := DRNotificationCenterCreate()
defer CFRelease(notification_center)
source := DRNotificationCenterCreateRunLoopSource(notification_center)
defer {
CFRunLoopSourceInvalidate(source)
CFRelease(source)
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes)
DRNotificationCenterAddObserver(notification_center, nil, auto_cast burn_handle_notification, nil, burner)
defer DRNotificationCenterRemoveObserver(notification_center, auto_cast burn_handle_notification, nil, burner)
burn_result := DRBurnWriteLayout(burner, disc_layout)
if burn_result != 0 {
burn_session_error("Failed to start burning. Error code: 0x%X", burn_result)
}
print("Starting burn!")
CFRunLoopRun();
}
create_drtrack :: proc(track: Track) -> DRTrackRef {
pregap_length, track_length := sector_count_of_track(track)
switch track.mode {
case .Audio:
return create_cdda_drtrack(pregap_length, track_length)
case .XAMode2Form1:
return create_mode2f1_drtrack(pregap_length, track_length)
case .Mode2, .Mode1:
burn_session_error("CDI contains unsupported track mode: %v", track.mode)
}
return nil
}
create_cdda_drtrack :: proc (pregap_length, track_length: int) -> DRTrackRef {
cfnum_track_length := cfnum(track_length)
defer CFRelease(cfnum_track_length)
cfnum_pregap_length := cfnum(pregap_length)
defer CFRelease(cfnum_pregap_length)
properties := cfdictionary(
kDRTrackLengthKey, cfnum_track_length,
kDRPreGapLengthKey, cfnum_pregap_length,
kDRBlockSizeKey, kDRBlockSizeAudio,
kDRBlockTypeKey, kDRBlockTypeAudio,
kDRDataFormKey, kDRDataFormAudio,
kDRSessionFormatKey, kDRSessionFormatAudio,
kDRTrackModeKey, kDRTrackModeAudio,
//kDRPreGapIsRequiredKey, kCFBooleanTrue,
)
defer CFRelease(properties)
print_verbose("Audio track properties: %v", properties)
return DRTrackCreate(properties, burn_callback)
}
create_mode2f1_drtrack :: proc (pregap_length, track_length: int) -> DRTrackRef {
cfnum_track_length := cfnum(track_length)
defer CFRelease(cfnum_track_length)
cfnum_pregap_length := cfnum(pregap_length)
defer CFRelease(cfnum_pregap_length)
properties := cfdictionary(
kDRTrackLengthKey, cfnum_track_length,
kDRPreGapLengthKey, cfnum_pregap_length,
kDRBlockSizeKey, kDRBlockSizeMode2Form1Data,
kDRBlockTypeKey, kDRBlockTypeMode2Form1Data,
kDRDataFormKey, kDRDataFormMode2Form1Data,
kDRSessionFormatKey, kDRSessionFormatCDXA,
kDRTrackModeKey, kDRTrackMode2Form1Data,
//kDRPreGapIsRequiredKey, kCFBooleanTrue,
)
defer CFRelease(properties)
print_verbose("Data track properties: %v", properties)
return DRTrackCreate(properties, burn_callback)
}