+void
+send_front_panel_update(struct NEDstate * state, int sockfd, uint32_t ram_address,
+ const struct sockaddr *to, socklen_t tolen)
+{
+ /*
+ * Datagram format (all entries are 4 bytes):
+ * Sequence number (See RFC1982)
+ * Currently executing word
+ * PC
+ * SC
+ * PSW
+ * SP
+ * Top 16 stack entries
+ * RAM start address
+ * Top 16 RAM words from start address
+ */
+ uint32_t snapshot[39];
+
+ static uint32_t seq_num;
+ seq_num == 0xffffffff ? seq_num = 0 : seq_num++;
+ snapshot[0] = seq_num;
+
+ snapshot[1] = ram_r_word(state, (state->active_thread->pc - 4));
+ snapshot[2] = state->active_thread->pc;
+ snapshot[3] = (uint32_t) state->active_thread->sc;
+ snapshot[4] = generate_binary_psw(state);
+ snapshot[5] = state->active_thread->sp;
+
+ int64_t sp = state->active_thread->sp;
+ for (int i=0; i<16; i++) {
+ if ((sp-i) < 0) {
+ snapshot[i+6] = 0;
+ } else {
+ snapshot[i+6] = state->active_thread->stack[sp-i];
+ }
+ }
+
+ snapshot[22] = ram_address;
+ for (int i=0; i<16; i++) {
+ snapshot[i+23] = ram_r_word(state,(ram_address + (i * 4)));
+ }
+
+ int numbytes;
+ if ((numbytes = sendto(sockfd, snapshot, 156, 0, to, tolen)) == -1) {
+ fprintf(stderr, "WARNING: Unable to send machine snapshot to front panel socket.\n");
+ }
+}