annotate oscilloscope.c @ 2283:6f6f21d0c396

Fix missing address masks on some VRAM reads
author Michael Pavone <pavone@retrodev.com>
date Mon, 09 Jan 2023 00:15:26 -0800
parents e27ab5f4bfe3
children d30ea441b92e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2245
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include "oscilloscope.h"
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include "render.h"
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include "blastem.h"
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #define INVALID_TRIGGER 0xFFFFFFFF
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #define WIDTH 1280
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #define HEIGHT 720
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 static void scope_window_close(uint8_t which)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 if (current_system && current_system->toggle_debug_view) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 current_system->toggle_debug_view(current_system, DEBUG_OSCILLOSCOPE);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 oscilloscope *create_oscilloscope()
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 oscilloscope *scope = calloc(1, sizeof(oscilloscope));
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 scope->channel_storage = 4;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 scope->channels = calloc(scope->channel_storage, sizeof(scope_channel));
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 scope->window = render_create_window("Oscilloscope", WIDTH, HEIGHT, scope_window_close);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 return scope;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 uint8_t scope_add_channel(oscilloscope *scope, const char *name, uint32_t sample_rate)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 if (scope->num_channels == scope->channel_storage) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 scope->channel_storage *= 2;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 scope->channels = realloc(scope->channels, scope->channel_storage * sizeof(scope_channel));
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 uint8_t channel = scope->num_channels++;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 scope_channel *chan = scope->channels + channel;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 chan->name = name;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 chan->period = sample_rate / 30;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 chan->next_sample = 0;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 chan->samples = calloc(chan->period, sizeof(int16_t));
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 chan->last_trigger = INVALID_TRIGGER;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 chan->cur_trigger = INVALID_TRIGGER;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 return channel;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 void scope_add_sample(oscilloscope *scope, uint8_t channel, int16_t value, uint8_t trigger)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 if (channel >= scope->num_channels) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 return;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 scope_channel *chan = scope->channels + channel;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 if (trigger) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 chan->cur_trigger = chan->next_sample;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 chan->samples[chan->next_sample++] = value;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 if (chan->next_sample == chan->period) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 chan->next_sample = 0;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 void scope_render(oscilloscope *scope)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 int pitch;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 uint32_t *fb = render_get_framebuffer(scope->window, &pitch);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 memset(fb, 0, HEIGHT * pitch);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 pitch /= sizeof(uint32_t);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 int offset = 0;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 int column_width = WIDTH/3;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 int width = column_width * 3;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 int row_height = HEIGHT / ((scope->num_channels + 2) / 3);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 float value_scale = (float)row_height / 20000.0f;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 uint32_t *cur_line = fb;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 for (uint8_t i = 0; i < scope->num_channels; i++)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 float samples_per_pixel = (float)scope->channels[i].period / (float)(2*column_width);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 uint32_t start = scope->channels[i].last_trigger;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 if (start == INVALID_TRIGGER) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 if (scope->channels[i].next_sample >= scope->channels[i].period / 2) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 start = scope->channels[i].next_sample - scope->channels[i].period / 2;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 } else {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 start = scope->channels[i].period - (scope->channels[i].period /2 - scope->channels[i].next_sample);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 scope->channels[i].last_trigger = scope->channels[i].cur_trigger;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 scope->channels[i].cur_trigger = INVALID_TRIGGER;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 float cur_sample = start;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 int last_y = -1;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 for (int x = offset; x < offset + column_width; x++)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 //TODO: bresenham
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 //TODO: at least linear filtering
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 int16_t sample = scope->channels[i].samples[(int)(cur_sample + 0.5f)];
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 int y = (float)sample * value_scale + 0.5f;
2275
e27ab5f4bfe3 Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents: 2245
diff changeset
87 if (y > row_height / 2 - 1) {
e27ab5f4bfe3 Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents: 2245
diff changeset
88 y = row_height / 2 - 1;
2245
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 } else if (y < -(row_height / 2)) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 y = -(row_height / 2);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 y += row_height / 2;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 if (last_y >= 0) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 int delta = last_y > y ? -1 : 1;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 while (last_y != y)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 cur_line[last_y * pitch + x ] = 0xFFFFFFFF;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 last_y += delta;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 } else {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 last_y = y;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 cur_line[y * pitch + x ] = 0xFFFFFFFF;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 cur_sample += samples_per_pixel;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 if (cur_sample + 0.5f >= scope->channels[i].period) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 cur_sample -= scope->channels[i].period;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 }
2275
e27ab5f4bfe3 Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents: 2245
diff changeset
109
2245
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 offset += column_width;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 if (offset >= width) {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 offset = 0;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 cur_line += pitch * row_height;
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 }
2275
e27ab5f4bfe3 Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents: 2245
diff changeset
116
e27ab5f4bfe3 Fix off by one in oscilloscope rendering
Michael Pavone <pavone@retrodev.com>
parents: 2245
diff changeset
117
2245
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 render_framebuffer_updated(scope->window, WIDTH);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 void scope_close(oscilloscope *scope)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 render_destroy_window(scope->window);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 for (uint8_t i = 0; i < scope->num_channels; i++)
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 {
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 free(scope->channels[i].samples);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 }
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 free(scope->channels);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 free(scope);
d220305e81b9 Add missing files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 }