/* message.c -- a message, as it is written to a port and read from one. * See message.h. Nothing here uses the C library. */ #include "v4/message.h" unsigned v4_message_text(v4_message *m, v4_cell to, v4_cell from, v4_cell type, const char *chars, unsigned len) { unsigned i, words; if (len > V4_MSG_MAX_CHARS) return 0; words = (len + 3u) / 4u; m->word[0] = to; m->word[1] = from; m->word[2] = type; m->word[3] = 0; m->word[4] = 0; m->word[5] = 0; m->word[6] = (v4_cell)len; for (i = 0; i < words; i++) m->word[V4_MSG_HEADER + i] = 0; for (i = 0; i < len; i++) m->word[V4_MSG_HEADER + i / 4u] = (v4_cell)((v4_ucell)m->word[V4_MSG_HEADER + i / 4u] | ((v4_ucell)(unsigned char)chars[i] << (8u * (i % 4u)))); m->count = V4_MSG_HEADER + words; return m->count; } unsigned v4_message_length(const v4_message *m) { v4_cell len = m->word[6]; return (len < 0 || len > (v4_cell)V4_MSG_MAX_CHARS) ? 0u : (unsigned)len; } char v4_message_char(const v4_message *m, unsigned i) { return (char)(((v4_ucell)m->word[V4_MSG_HEADER + i / 4u] >> (8u * (i % 4u))) & 0xFFu); } int v4_message_word(v4_message *m, v4_cell word) { if (m->count < V4_MSG_MAX_WORDS) m->word[m->count] = word; m->count++; if (m->count < V4_MSG_HEADER) return 0; return m->count >= V4_MSG_HEADER + (v4_message_length(m) + 3u) / 4u; }