summaryrefslogtreecommitdiff
path: root/oscReceiveExample/mame
diff options
context:
space:
mode:
authorTim Redfern <tim@getdrop.com>2018-12-12 01:17:00 +0000
committerTim Redfern <tim@getdrop.com>2018-12-12 01:17:00 +0000
commit983d4a8a596a680edc9a1bc78a6b5b61b6486be5 (patch)
tree12c7616bd326fb773d61b843b52ee1c8961ecebf /oscReceiveExample/mame
parentd59dff2cc79b63962009786cc19223fa5a197757 (diff)
mame vector receiver
Diffstat (limited to 'oscReceiveExample/mame')
-rw-r--r--oscReceiveExample/mame/vector.cpp264
-rw-r--r--oscReceiveExample/mame/vector.h71
2 files changed, 335 insertions, 0 deletions
diff --git a/oscReceiveExample/mame/vector.cpp b/oscReceiveExample/mame/vector.cpp
new file mode 100644
index 0000000..89c27c2
--- /dev/null
+++ b/oscReceiveExample/mame/vector.cpp
@@ -0,0 +1,264 @@
+// license:BSD-3-Clause
+// copyright-holders:Brad Oliver,Aaron Giles,Bernd Wiebelt,Allard van der Bas
+/******************************************************************************
+ *
+ * vector.c
+ *
+ * anti-alias code by Andrew Caldwell
+ * (still more to add)
+ *
+ * 040227 Fixed miny clip scaling which was breaking in mhavoc. AREK
+ * 010903 added support for direct RGB modes MLR
+ * 980611 use translucent vectors. Thanks to Peter Hirschberg
+ * and Neil Bradley for the inspiration. BW
+ * 980307 added cleverer dirty handling. BW, ASG
+ * fixed antialias table .ac
+ * 980221 rewrote anti-alias line draw routine
+ * added inline assembly multiply fuction for 8086 based machines
+ * beam diameter added to draw routine
+ * beam diameter is accurate in anti-alias line draw (Tcosin)
+ * flicker added .ac
+ * 980203 moved LBO's routines for drawing into a buffer of vertices
+ * from avgdvg.c to this location. Scaling is now initialized
+ * by calling vector_init(...). BW
+ * 980202 moved out of msdos.c ASG
+ * 980124 added anti-alias line draw routine
+ * modified avgdvg.c and sega.c to support new line draw routine
+ * added two new tables Tinten and Tmerge (for 256 color support)
+ * added find_color routine to build above tables .ac
+ *
+ * Vector Team
+ *
+ * Brad Oliver
+ * Aaron Giles
+ * Bernd Wiebelt
+ * Allard van der Bas
+ * Al Kossow (VECSIM)
+ * Hedley Rainnie (VECSIM)
+ * Eric Smith (VECSIM)
+ * Neil Bradley (technical advice)
+ * Andrew Caldwell (anti-aliasing)
+ *
+ **************************************************************************** */
+
+#include "emu.h"
+#include "emuopts.h"
+#include "rendutil.h"
+#include "vector.h"
+
+//make SUBTARGET=arcade
+
+//#include "libheliosdac/HeliosDacAPI.h"
+#include "osc/OscOutboundPacketStream.h"
+
+#include "ip/IpEndpointName.h"
+
+#define ADDRESS "127.0.0.1"
+#define PORT 7070
+
+#define VECTOR_WIDTH_DENOM 512
+
+#define MAX_POINTS 10000
+
+float vector_options::s_flicker = 0.0f;
+float vector_options::s_beam_width_min = 0.0f;
+float vector_options::s_beam_width_max = 0.0f;
+float vector_options::s_beam_intensity_weight = 0.0f;
+
+void vector_options::init(emu_options& options)
+{
+ s_beam_width_min = options.beam_width_min();
+ s_beam_width_max = options.beam_width_max();
+ s_beam_intensity_weight = options.beam_intensity_weight();
+ s_flicker = options.flicker();
+}
+
+// device type definition
+DEFINE_DEVICE_TYPE(VECTOR, vector_device, "vector_device", "VECTOR")
+
+vector_device::vector_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
+ : device_t(mconfig, VECTOR, tag, owner, clock),
+ device_video_interface(mconfig, *this),
+ m_vector_list(nullptr),
+ m_min_intensity(255),
+ m_max_intensity(0),
+ transmitSocket(IpEndpointName( ADDRESS, PORT ))
+{
+}
+
+void vector_device::device_start()
+{
+ vector_options::init(machine().options());
+
+ m_vector_index = 0;
+
+ /* allocate memory for tables */
+ m_vector_list = make_unique_clear<point[]>(MAX_POINTS);
+}
+
+/*
+ * www.dinodini.wordpress.com/2010/04/05/normalized-tunable-sigmoid-functions/
+ */
+float vector_device::normalized_sigmoid(float n, float k)
+{
+ // valid for n and k in range of -1.0 and 1.0
+ return (n - n * k) / (k - fabs(n) * 2.0f * k + 1.0f);
+}
+
+
+/*
+ * Adds a line end point to the vertices list. The vector processor emulation
+ * needs to call this.
+ */
+void vector_device::add_point(int x, int y, rgb_t color, int intensity)
+{
+ point *newpoint;
+
+ intensity = std::max(0, std::min(255, intensity));
+
+ m_min_intensity = intensity > 0 ? std::min(m_min_intensity, intensity) : m_min_intensity;
+ m_max_intensity = intensity > 0 ? std::max(m_max_intensity, intensity) : m_max_intensity;
+
+ if (vector_options::s_flicker && (intensity > 0))
+ {
+ float random = (float)(machine().rand() & 255) / 255.0f; // random value between 0.0 and 1.0
+
+ intensity -= (int)(intensity * random * vector_options::s_flicker);
+
+ intensity = std::max(0, std::min(255, intensity));
+ }
+
+ newpoint = &m_vector_list[m_vector_index];
+ newpoint->x = x;
+ newpoint->y = y;
+ newpoint->col = color;
+ newpoint->intensity = intensity;
+
+ m_vector_index++;
+ if (m_vector_index >= MAX_POINTS)
+ {
+ m_vector_index--;
+ logerror("*** Warning! Vector list overflow!\n");
+ }
+}
+
+
+/*
+ * The vector CPU creates a new display list. We save the old display list,
+ * but only once per refresh.
+ */
+void vector_device::clear_list(void)
+{
+ m_vector_index = 0;
+}
+
+#define OSC_BUFFER_SIZE 65535
+#define OSC_PACKET_LENGTH 5
+
+uint32_t vector_device::screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
+{
+ uint32_t flags = PRIMFLAG_ANTIALIAS(1) | PRIMFLAG_BLENDMODE(BLENDMODE_ADD) | PRIMFLAG_VECTOR(1);
+ const rectangle &visarea = screen.visible_area();
+ float xscale = 1.0f / (65536 * visarea.width());
+ float yscale = 1.0f / (65536 * visarea.height());
+ float xoffs = (float)visarea.min_x;
+ float yoffs = (float)visarea.min_y;
+
+ point *curpoint;
+ int lastx = 0;
+ int lasty = 0;
+
+ curpoint = m_vector_list.get();
+
+ screen.container().empty();
+ screen.container().add_rect(0.0f, 0.0f, 1.0f, 1.0f, rgb_t(0xff,0x00,0x00,0x00), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA) | PRIMFLAG_VECTORBUF(1));
+
+ float m1,m2,m3,m4;
+ m1=m2=m3=m4=0.0f;
+
+ char buffer[OSC_BUFFER_SIZE];
+ osc::OutboundPacketStream p( buffer, OSC_BUFFER_SIZE );
+
+
+ int MAX_PACKETS=OSC_BUFFER_SIZE/(OSC_PACKET_LENGTH*sizeof(float));
+
+ //theres a limit of ~54000 bytes for a udp packet this way
+ //hmm, this is failing with far less, like 14k
+
+ int recorded=0;
+
+ float oscdata[m_vector_index*5];
+ int osccounter=0;
+
+ for (int i = 0; i < m_vector_index; i++)
+ {
+ render_bounds coords;
+
+ float intensity = (float)curpoint->intensity / 255.0f;
+ float intensity_weight = normalized_sigmoid(intensity, vector_options::s_beam_intensity_weight);
+
+ // check for static intensity
+ float beam_width = m_min_intensity == m_max_intensity
+ ? vector_options::s_beam_width_min
+ : vector_options::s_beam_width_min + intensity_weight * (vector_options::s_beam_width_max - vector_options::s_beam_width_min);
+
+ // normalize width
+ beam_width *= 1.0f / (float)VECTOR_WIDTH_DENOM;
+
+ coords.x0 = ((float)lastx - xoffs) * xscale;
+ coords.y0 = ((float)lasty - yoffs) * yscale;
+ coords.x1 = ((float)curpoint->x - xoffs) * xscale;
+ coords.y1 = ((float)curpoint->y - yoffs) * yscale;
+
+ if (coords.x0<m1) m1=coords.x0;
+ if (coords.y0<m2) m2=coords.y0;
+ if (coords.x0>m3) m3=coords.x0;
+ if (coords.y0>m4) m4=coords.y0;
+
+ if ((recorded<MAX_PACKETS)&&((coords.x0!=coords.x1)||(coords.y0!=coords.y1))){
+ oscdata[osccounter]=(coords.x0);
+ oscdata[++osccounter]=(coords.y0);
+ oscdata[++osccounter]=(beam_width);
+ *((uint32_t*)(&oscdata[++osccounter]))=(osc::int32)((curpoint->intensity << 24) | (curpoint->col & 0xffffff));
+ osccounter++;
+ //p << coords.x1 << coords.y1 << beam_width;
+ //p << 1.0f; //(osc::int32)((curpoint->intensity << 24) | (curpoint->col & 0xffffff));
+ recorded++;
+ }
+
+ if (curpoint->intensity != 0)
+ {
+ screen.container().add_line(
+ coords.x0, coords.y0, coords.x1, coords.y1,
+ beam_width,
+ (curpoint->intensity << 24) | (curpoint->col & 0xffffff),
+ flags);
+ }
+
+ lastx = curpoint->x;
+ lasty = curpoint->y;
+
+ curpoint++;
+
+ //std::cout << std::hex << (osc::int32)((curpoint->intensity << 24) | (curpoint->col & 0xffffff)) << std::endl;
+ }
+
+ //(osc::int32)((curpoint->intensity << 24) | (curpoint->col & 0xffffff)
+
+ /*
+ Tims-MacBook-Pro:mame tim$ sudo sysctl -w net.inet.udp.maxdgram=65535
+ Password:
+ net.inet.udp.maxdgram: 9216 -> 65535
+ */
+ osc::Blob points=osc::Blob(oscdata,recorded*sizeof(float)*4);
+
+ p << osc::BeginBundleImmediate << osc::BeginMessage( "/points" ) << recorded << points << osc::EndMessage << osc::EndBundle;
+
+ transmitSocket.Send( p.Data(), p.Size() );
+
+ //if (recorded<m_vector_index){
+ //std::cout << "Capacity: "<< p.Capacity() << " Sending " << p.Size() << " bytes, " << recorded << " packets of " << m_vector_index << std::endl;
+ //}
+
+ return 0;
+}
diff --git a/oscReceiveExample/mame/vector.h b/oscReceiveExample/mame/vector.h
new file mode 100644
index 0000000..658789e
--- /dev/null
+++ b/oscReceiveExample/mame/vector.h
@@ -0,0 +1,71 @@
+// license:BSD-3-Clause
+// copyright-holders:Brad Oliver,Aaron Giles,Bernd Wiebelt,Allard van der Bas
+#ifndef MAME_VIDEO_VECTOR_H
+#define MAME_VIDEO_VECTOR_H
+
+#pragma once
+
+#include "ip/UdpSocket.h"
+
+class vector_device;
+
+class vector_options
+{
+public:
+ friend class vector_device;
+
+ static float s_flicker;
+ static float s_beam_width_min;
+ static float s_beam_width_max;
+ static float s_beam_intensity_weight;
+
+protected:
+ static void init(emu_options& options);
+};
+
+class vector_device : public device_t, public device_video_interface
+{
+public:
+ template <typename T> static constexpr rgb_t color111(T c) { return rgb_t(pal1bit(c >> 2), pal1bit(c >> 1), pal1bit(c >> 0)); }
+ template <typename T> static constexpr rgb_t color222(T c) { return rgb_t(pal2bit(c >> 4), pal2bit(c >> 2), pal2bit(c >> 0)); }
+ template <typename T> static constexpr rgb_t color444(T c) { return rgb_t(pal4bit(c >> 8), pal4bit(c >> 4), pal4bit(c >> 0)); }
+
+ // construction/destruction
+ vector_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
+
+ uint32_t screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
+ void clear_list();
+
+ void add_point(int x, int y, rgb_t color, int intensity);
+
+ // device-level overrides
+ virtual void device_start() override;
+
+private:
+ /* The vertices are buffered here */
+ struct point
+ {
+ point() : x(0), y(0), col(0), intensity(0) { }
+
+ int x; int y;
+ rgb_t col;
+ int intensity;
+ };
+
+ std::unique_ptr<point[]> m_vector_list;
+ int m_vector_index;
+ int m_min_intensity;
+ int m_max_intensity;
+
+ float normalized_sigmoid(float n, float k);
+
+ UdpTransmitSocket transmitSocket;
+};
+
+// device type definition
+DECLARE_DEVICE_TYPE(VECTOR, vector_device)
+
+#define MCFG_VECTOR_ADD(_tag) \
+ MCFG_DEVICE_ADD(_tag, VECTOR, 0)
+
+#endif // MAME_VIDEO_VECTOR_H